【问题标题】:How to modify wordpress comment form如何修改wordpress评论表单
【发布时间】:2018-05-21 12:15:58
【问题描述】:

我需要在 wordpress 评论表单中插入包装器。我还需要向文本区域、输入字段添加类,并且我需要在标准的 wordpress 评论表单中添加另一个输入字段。现在我正在我的functions.php中尝试以下内容,但没有任何改变,有人可以帮忙吗?

add_filter( 'comment_form_default_fields', 'crb_custom_fields', 2 );
function crb_custom_fields() {
$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );

$custom_fields =  array(
    'author' =>
    '<p class="comment-form-author"><label for="author">' . __( 'Name', 'crb' ) .
    ( $req ? '<span class="required">*</span>' : '' ) . '</label>' .
    '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .
    '" size="30"' . $aria_req . ' /></p>',

    'email' =>
    '<p class="comment-form-email"><label for="email">' . __( 'Email', 'crb' ) .
    ( $req ? '<span class="required">*</span>' : '' ) . '</label>' .
    '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) .
    '" size="30"' . $aria_req . ' /></p>',

    'url' =>
    '<p class="comment-form-url"><label for="url">' . __( 'Website', 'crb' ) . '</label>' .
    '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .
    '" size="30" /></p>',

    'comment_field' =>  '<p class="field comment-form-comment"><label for="comment" class="hidden">' . _x( 'Comment', 'noun' ) .
    '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true">' .
    '</textarea></p>',
);

return $custom_fields;
}

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您在过滤器挂钩中传递了两个参数,但是当您定义函数时,您实际上并没有传递任何参数。

    [...]
    
      add_filter( 'comment_form_default_fields', 'crb_custom_fields', 10, 1 );
      function crb_custom_fields( $fields ) {
    
    [...]
    

    改用上面的。我已将优先级设置为 10(默认值)并将参数数量设置为 1。如果您愿意,可以将其省略,但它会让您的设置更加冗长。

    我还添加了在回调时传递给函数的$fields 参数。这是一个包含联系表单 HTML 的数组。因此,您可以将实际代码更改为以下内容,您应该会开始看到结果。

    add_filter( 'comment_form_default_fields', 'crb_custom_fields', 10, 1 );
    
    function crb_custom_fields( $fields ) {
    
    $commenter = wp_get_current_commenter();
    $req = get_option( 'require_name_email' );
    $aria_req = ( $req ? " aria-required='true'" : '' );
    
    $fields =  array(
        'author' =>
        '<p class="comment-form-author"><label for="author">' . __( 'Name', 'crb' ) .
        ( $req ? '<span class="required">*</span>' : '' ) . '</label>' .
        '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .
        '" size="30"' . $aria_req . ' /></p>',
    
        'email' =>
        '<p class="comment-form-email"><label for="email">' . __( 'Email', 'crb' ) .
        ( $req ? '<span class="required">*</span>' : '' ) . '</label>' .
        '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) .
        '" size="30"' . $aria_req . ' /></p>',
    
        'url' =>
        '<p class="comment-form-url"><label for="url">' . __( 'Website', 'crb' ) . '</label>' .
        '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .
        '" size="30" /></p>',
    
        'comment_field' =>  '<p class="field comment-form-comment"><label for="comment" class="hidden">' . _x( 'Comment', 'noun' ) .
        '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true">' .
        '</textarea></p>',
    );
    
    return $custom_fields;
    
    }
    

    然后,此过滤器会改变 comment_form() 函数打印输出的外观(HTML)。这是一个通过钩子非常可定制的功能。我建议阅读文档以获取更多信息。 comment_form_default_fields 钩子文档也很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-23
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 2012-02-01
      • 1970-01-01
      相关资源
      最近更新 更多