the_block_editor_meta_box_post_form_hidden_fields()


WordPressの関数the_block_editor_meta_box_post_form_hidden_fields()は、ブロックエディタ内のメタボックスに隠しフィールドを出力するために使用されます。

構文

the_block_editor_meta_box_post_form_hidden_fields( int $post_id );
  • $post_id(int)—隠しフィールドを追加する投稿のID。

例1:投稿編集画面に隠しフィールドを追加

投稿編集画面に基本的な隠しフィールドを追加します。

the_block_editor_meta_box_post_form_hidden_fields(get_the_ID());

例2:カスタム投稿タイプで使用

カスタム投稿タイプの編集画面に隠しフィールドを追加します。

the_block_editor_meta_box_post_form_hidden_fields($post->ID);

例3:特定の投稿IDを指定

固定の投稿IDを指定して隠しフィールドを出力します。

the_block_editor_meta_box_post_form_hidden_fields(123);

例4:フックと組み合わせて使用

admin_headフックと組み合わせて使用します。

add_action('admin_head',function(){the_block_editor_meta_box_post_form_hidden_fields(get_the_ID());});

例5:条件分岐と共に使用

特定の条件下でのみ隠しフィールドを出力します。

if(is_admin()){the_block_editor_meta_box_post_form_hidden_fields(get_the_ID());}

例6:複数の隠しフィールドをカスタマイズ

複数のカスタムフィールドを追加する前処理を行います。

do_action('before_hidden_fields');the_block_editor_meta_box_post_form_hidden_fields(get_the_ID());

例7:ショートコード内で使用

ショートコードのコールバック関数内で使用します。

add_shortcode('custom_hidden_fields',function(){ob_start();the_block_editor_meta_box_post_form_hidden_fields(get_the_ID());return ob_get_clean();});

注意点

  • この関数は管理者画面でのみ動作します。
  • Gutenbergエディタが有効な環境で使用してください。
  • 投稿オブジェクトが存在しない場合にエラーが発生する可能性があります。

関連機能: