钩子文档

block_editor_meta_box_hidden_fields

💡 云策文档标注

概述

此文档介绍 WordPress 的 block_editor_meta_box_hidden_fields 钩子,用于在元框保存表单中添加隐藏输入字段。开发者可通过此钩子输出字段,以便在保存元框时 POST 回服务器。

关键要点

  • 钩子名称:block_editor_meta_box_hidden_fields
  • 参数:$post(WP_Post 对象,表示正在编辑的文章)
  • 用途:允许开发者在元框保存表单中打印隐藏字段,用于后端处理
  • 引入版本:WordPress 5.0.0
  • 相关函数:the_block_editor_meta_box_post_form_hidden_fields(),位于 wp-admin/includes/post.php

代码示例

add_action( 'block_editor_meta_box_hidden_fields', 'add_gutenberg_hidden_fields', 10, 1 );
function add_gutenberg_hidden_fields( $post ) {
    echo '<input type="hidden" name="is_gutenberg_post" value="1">';
    echo '<input type="hidden" name="another_field" value="some_value">';
}

注意事项

在 save_post 钩子中,可以通过检查 $_POST['is_gutenberg_post'] 来判断文章是否通过 Gutenberg 编辑器发布。


📄 原文内容

Adds hidden input fields to the meta box save form.

Description

Hook into this action to print <input type="hidden" ... /> fields, which will be POSTed back to the server when meta boxes are saved.

Parameters

$postWP_Post
The post that is being edited.

Source

do_action( 'block_editor_meta_box_hidden_fields', $post );

Changelog

Version Description
5.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    In save_post hook, to check if the post is published via gutenberg, use this

    add_action( 'block_editor_meta_box_hidden_fields', 'add_gutenberg_hidden_fields', 10, 1 );
    function add_gutenberg_hidden_fields( $post ) {
    
    	echo '<input type="hidden" name="your-field" value="some-value">';
    	echo '<input type="hidden" name="is_gutenberg_post" value="1">';
    }

    then inside save_post callback, use

    if ( isset( $_POST['is_gutenberg_post'] ) ) {
        // came from gutengerg
    }