the_block_editor_meta_box_post_form_hidden_fields()
云策文档标注
概述
该函数用于在块编辑器(Block Editor)中渲染元框(meta box)表单所需的隐藏字段,确保向后兼容经典编辑器(Classic Editor)的钩子行为。它处理当前文章对象,生成包括 nonce、referer 和用户 ID 等关键隐藏输入。
关键要点
- 函数 the_block_editor_meta_box_post_form_hidden_fields() 接收一个 WP_Post 对象作为参数,用于生成隐藏表单字段。
- 它通过 wp_nonce_field() 生成安全 nonce 字段,并包含 referer、用户 ID、文章类型和状态等隐藏输入。
- 为了向后兼容,函数捕获 edit_form_after_title 和 edit_form_advanced 钩子的输出,从中提取隐藏输入字段。
- 函数触发 block_editor_meta_box_hidden_fields 钩子,允许开发者添加自定义隐藏字段。
- 自 WordPress 5.0.0 版本引入,主要用于 the_block_editor_meta_boxes() 函数中渲染元框表单。
代码示例
function the_block_editor_meta_box_post_form_hidden_fields( $post ) {
$form_extra = '';
if ( 'auto-draft' === $post->post_status ) {
$form_extra .= "";
}
$form_action = 'editpost';
$nonce_action = 'update-post_' . $post->ID;
$form_extra .= "<input type='hidden' id='original_post_status' value='" . esc_attr( $post->post_status ) . "' />";
$referer = wp_get_referer();
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
wp_nonce_field( $nonce_action );
ob_start();
do_action( 'edit_form_after_title', $post );
do_action( 'edit_form_advanced', $post );
$classic_output = ob_get_clean();
$classic_elements = wp_html_split( $classic_output );
$hidden_inputs = '';
foreach ( $classic_elements as $element ) {
if ( ! str_starts_with( $element, '<input' ) ) {
continue;
}
if ( false === strpos( $element, "type='hidden'" ) && false === strpos( $element, 'type="hidden"' ) ) {
continue;
}
$hidden_inputs .= $element;
}
echo $form_extra;
echo $hidden_inputs;
echo "<input type='hidden' id='user-id' name='user_ID' value='" . esc_attr( $user_id ) . "' />";
echo "<input type='hidden' id='hiddenaction' name='action' value='" . esc_attr( $form_action ) . "' />";
echo "<input type='hidden' id='originalaction' name='originalaction' value='" . esc_attr( $form_action ) . "' />";
echo "<input type='hidden' id='post_type' name='post_type' value='" . esc_attr( $post->post_type ) . "' />";
echo "<input type='hidden' id='post_status' name='post_status' value='" . esc_attr( get_post_status( $post ) ) . "' />";
echo "<input type='hidden' id='referredby' name='referredby' value='" . esc_url( $referer ) . "' />";
if ( ! empty( $referer ) ) {
wp_original_referer_field( true, 'previous' );
}
do_action( 'block_editor_meta_box_hidden_fields', $post );
}注意事项
- 该函数专为块编辑器设计,确保元框保存时能正确 POST 数据到服务器。
- 使用 wp_html_split() 解析经典钩子输出,仅提取隐藏输入字段,避免引入其他 HTML 元素。
- 开发者可以通过 block_editor_meta_box_hidden_fields 钩子添加自定义隐藏字段,以扩展功能。
原文内容
Renders the hidden form required for the meta boxes form.
Parameters
$postWP_Postrequired-
Current post object.
Source
function the_block_editor_meta_box_post_form_hidden_fields( $post ) {
$form_extra = '';
if ( 'auto-draft' === $post->post_status ) {
$form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />";
}
$form_action = 'editpost';
$nonce_action = 'update-post_' . $post->ID;
$form_extra .= "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_attr( $post->ID ) . "' />";
$referer = wp_get_referer();
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
wp_nonce_field( $nonce_action );
/*
* Some meta boxes hook into these actions to add hidden input fields in the classic post form.
* For backward compatibility, we can capture the output from these actions,
* and extract the hidden input fields.
*/
ob_start();
/** This filter is documented in wp-admin/edit-form-advanced.php */
do_action( 'edit_form_after_title', $post );
/** This filter is documented in wp-admin/edit-form-advanced.php */
do_action( 'edit_form_advanced', $post );
$classic_output = ob_get_clean();
$classic_elements = wp_html_split( $classic_output );
$hidden_inputs = '';
foreach ( $classic_elements as $element ) {
if ( ! str_starts_with( $element, '<input ' ) ) {
continue;
}
if ( preg_match( '/stype=['"]hidden['"]s/', $element ) ) {
echo $element;
}
}
?>
<input type="hidden" id="user-id" name="user_ID" value="<?php echo (int) $user_id; ?>" />
<input type="hidden" id="hiddenaction" name="action" value="<?php echo esc_attr( $form_action ); ?>" />
<input type="hidden" id="originalaction" name="originalaction" value="<?php echo esc_attr( $form_action ); ?>" />
<input type="hidden" id="post_type" name="post_type" value="<?php echo esc_attr( $post->post_type ); ?>" />
<input type="hidden" id="original_post_status" name="original_post_status" value="<?php echo esc_attr( $post->post_status ); ?>" />
<input type="hidden" id="referredby" name="referredby" value="<?php echo $referer ? esc_url( $referer ) : ''; ?>" />
` fields, which will be POSTed back to
* the server when meta boxes are saved.
*
* @since 5.0.0
*
* @param WP_Post $post The post that is being edited.
*/
do_action( 'block_editor_meta_box_hidden_fields', $post );
}
Hooks
- do_action( ‘block_editor_meta_box_hidden_fields’, WP_Post $post )
-
Adds hidden input fields to the meta box save form.
- do_action( ‘edit_form_advanced’, WP_Post $post )
-
Fires after ‘normal’ context meta boxes have been output for all post types other than ‘page’.
- do_action( ‘edit_form_after_title’, WP_Post $post )
-
Fires after the title field.
Changelog
| Version | Description |
|---|---|
| 5.0.0 | Introduced. |