register_block_core_post_comments_form()


WordPressのregister_block_core_post_comments_form()関数は、コメントフォームブロックを登録するために使用されます。この関数は、ブロックエディタ内でコメントフォームを表示するためのブロックを登録します。

構文

register_block_core_post_comments_form();

例1: コメントフォームブロックの登録

この例では、コメントフォームブロックを登録します。

add_action( 'init', function() { register_block_core_post_comments_form(); } );

例2: カスタムブロックエディタでの使用

カスタムブロックエディタにコメントフォームブロックを追加します。

register_block_type( 'core/post-comments-form', array( 'editor_script' => 'wp-block-editor' ) );

例3: コメントフォームのスタイル変更

コメントフォームのスタイルを変更するために、カスタムCSSを追加します。

wp_enqueue_style( 'custom-comment-form-style', get_template_directory_uri() . '/css/custom-comment-form.css' );

例4: コメントフォームの表示条件を設定

特定の条件下でのみコメントフォームを表示します。

if ( comments_open() ) { register_block_core_post_comments_form(); }

例5: コメントフォームのテキスト変更

コメントフォームのテキストをカスタマイズします。

add_filter( 'comment_form_defaults', function( $defaults ) { $defaults['title_reply'] = 'コメントを投稿'; return $defaults; } );

例6: コメントフォームのフィールド追加

コメントフォームに新しいフィールドを追加します。

add_filter( 'comment_form_default_fields', function( $fields ) { $fields['custom_field'] = '<input type="text" name="custom_field" />'; return $fields; } );

例7: コメントフォームのバリデーション追加

コメントフォームにカスタムバリデーションを追加します。

add_filter( 'preprocess_comment', function( $commentdata ) { if ( empty( $commentdata['custom_field'] ) { wp_die( 'カスタムフィールドを入力してください。' ); } return $commentdata; } );

注意点

  • register_block_core_post_comments_form()は、WordPressのブロックエディタでのみ使用されます。
  • カスタマイズを行う際は、テーマやプラグインとの互換性に注意してください。

関連機能: