如何修改wordpress评论表单

时间:2018-05-21 12:15:58

标签: php wordpress

我需要在wordpress评论表单中插入包装器。另外我需要在textarea,输入字段中添加类,还需要在标准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;
}

1 个答案:

答案 0 :(得分:0)

你在过滤器钩子中传递了两个参数,但是当你定义你的函数时,你根本就没有传递任何参数。

[...]

  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挂钩文档也很有用。

相关问题