钩子文档

comment_form_defaults

💡 云策文档标注

概述

comment_form_defaults 是一个 WordPress 过滤器,用于修改评论表单的默认参数。开发者可以通过此过滤器自定义表单的标题、字段等设置,以适配特定主题或功能需求。

关键要点

  • 这是一个过滤器,允许修改评论表单的默认参数数组 $defaults。
  • 常用于主题的 functions.php 文件中,通过 add_filter 挂钩来应用自定义设置。
  • 可修改的参数包括 title_reply、fields、comment_field 等,具体列表在文档中提供。

代码示例

function wpdocs_set_comment_form_defaults( $defaults ) {
    $defaults['title_reply'] = __( 'Add a Comment' );
    return $defaults;
}
add_filter( 'comment_form_defaults', 'wpdocs_set_comment_form_defaults' );

📄 原文内容

Filters the comment form default arguments.

Description

Use ‘comment_form_default_fields’ to filter the comment fields.

Parameters

$defaultsarray
The default comment form arguments.

Source

$args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    The following defaults are possible to set with this code:

    function wpdocs_set_comment_form_defaults( $defaults ) {
    	//Here you are able to change the $defaults[]
    	//For example: 
    	$defaults['title_reply'] = __( 'Add a Comment' );
    	
    	return $defaults;
    }
    add_filter( 'comment_form_defaults', 'wpdocs_set_comment_form_defaults' );

    Possible values for $defaults[ ‘…’ ]:

    • fields

      • author
      • email
      • url
      • cookies
      • comment
    • comment_field
    • must_log_in
    • logged_in_as
    • comment_notes_before
    • comment_notes_after
    • action
    • id_form
    • id_submit
    • name_submit
    • title_reply
    • title_reply_to
    • title_reply_before
    • title_reply_after
    • cancel_reply_before
    • cancel_reply_after
    • cancel_reply_link
    • label_submit
    • submit_button
    • submit_field
    • format

  2. Skip to note 4 content

    Example for overriding the title of the comment form (default ‘Leave a Reply’) from within a theme’s functions.php in the theme setup function:

      function wpdocs_comment_form_defaults( $defaults ) {
        $defaults['title_reply'] = __( 'Add a Comment' );
        return $defaults;
      }
      add_filter( 'comment_form_defaults', 'wpdocs_comment_form_defaults' );