如何在WordPress Comment_Form中取消设置COMMENT_NOTES_BEFORE字段

时间:2014-03-26 作者:Noumaan Yaqoob

我正在尝试删除Your email address will not be published. 第二十三和第十四个默认主题中注释表单的行。

我试过这个:

function alter_comment_form_fields($fields){
unset($fields[\'comment_notes_before\']);
return $fields;
}
add_filter(\'comment_form_default_fields\',\'alter_comment_form_fields\');
还有这个:

function alter_comment_form_fields($fields){
$fields[\'comment_notes_before\'] = \'<p>Email address not required.</p>\';
return $fields;
}
add_filter(\'comment_form_default_fields\',\'alter_comment_form_fields\');
似乎什么都没用。我也找到了这张票https://core.trac.wordpress.org/ticket/14510 如果有人能给我指点方向,我将不胜感激。

2 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

你的代码在这里是完全错误的。你的主意不错。如果查看默认值comment_form, 你会看到$fields 包括以下内容、作者、电子邮件和URL。

这就是为什么你的代码不起作用,就像comment_notes_before 不在$field 领域

我个人认为,正确的方法是复制comment_form() 对于你的主题,重命名它并在其中进行所有更改,然后将其挂回适当的过滤器。

我在这里的理由是,首先,对comment_form() 在您的主题范围内。其次,我使用的是基于jquery的验证系统,为了使其正常工作,这里的方法是复制comment_form(). 这是经过考验的,你可以看到我提出的问题here 关于这个特定的主题。

所以说了这么多之后,这里是我目前在我的主题中使用的内容。您将需要完全相同的操作。你只需要改变你想要的,去需要你想要的。

 function pietergoosen_comment_form_fields( $args = array(), $post_id = null ) {
    if ( null === $post_id )
        $post_id = get_the_ID();
    else
        $id = $post_id;

    $commenter = wp_get_current_commenter();
    $user = wp_get_current_user();
    $user_identity = $user->exists() ? $user->display_name : \'\';

    $args = wp_parse_args( $args );
    if ( ! isset( $args[\'format\'] ) )
        $args[\'format\'] = current_theme_supports( \'html5\', \'comment-form\' ) ? \'html5\' : \'xhtml\';

    $req      = get_option( \'require_name_email\' );
    $aria_req = ( $req ? " aria-required=\'true\'" : \'\' );
    $html5    = \'html5\' === $args[\'format\'];
    $fields   =  array(
         \'author\' =>
    \'<p class="comment-form-author"><label for="author">\' . __( \'Name\', \'domainreference\' ) . \'</label> \' .
    ( $req ? \'<span class="required">*</span>\' : \'\' ) .
    \'<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\', \'domainreference\' ) . \'</label> \' .
    ( $req ? \'<span class="required">*</span>\' : \'\' ) .
    \'<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\', \'domainreference\' ) . \'</label>\' .
    \'<input id="url" name="url" type="text" value="\' . esc_attr( $commenter[\'comment_author_url\'] ) .
    \'" size="30" /></p>\',
);

    $required_text = sprintf( \' \' . __( \'Required fiels are marked %s\', \'pietergoosen\' ), \'<span class="required">*</span>\' );

    $fields = apply_filters( \'comment_form_default_fields\', $fields );
    $defaults = array(
        \'fields\'               => $fields,
        \'comment_field\'        => \'<p class="comment-form-comment"><label for="comment">\' . _x( \'Comment\', \'Comments field name\', \'pietergoosen\' ) . \'</label> <textarea id="comment" name="comment" placeholder="\'.__( \'*Required* Enter your comment here. Minimum 20 characters, please\', \'pietergoosen\' ).\'" cols="45" rows="8" aria-required="true"></textarea></p>\',
        \'must_log_in\'          => \'<p class="must-log-in">\' . sprintf( __( \'You must be <a href="%s">logged in</a> to post a comment.\', \'pietergoosen\' ), wp_login_url( apply_filters( \'the_permalink\', get_permalink( $post_id ) ) ) ) . \'</p>\',
        \'logged_in_as\'         => \'<p class="logged-in-as">\' . sprintf( __( \'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>\', \'pietergoosen\' ), get_edit_user_link(), $user_identity, wp_logout_url( apply_filters( \'the_permalink\', get_permalink( $post_id ) ) ) ) . \'</p>\',
        \'comment_notes_before\' => \'<p class="comment-notes">\' . __( \'Your email address will not be published.\', \'pietergoosen\' ) . ( $req ? $required_text : \'\' ) . \'</p>\',
        \'comment_notes_after\'  => \'<p class="form-allowed-tags">\' . sprintf( __( \'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s\', \'pietergoosen\' ), \' <code>\' . allowed_tags() . \'</code>\' ) . \'</p>\',
        \'id_form\'              => \'commentform\',
        \'id_submit\'            => \'submit\',
        \'title_reply\'          => __( \'Leave a Reply\', \'pietergoosen\' ),
        \'title_reply_to\'       => __( \'Leave a Reply to %s\', \'pietergoosen\' ),
        \'cancel_reply_link\'    => __( \'Cancel reply\', \'pietergoosen\' ),
        \'label_submit\'         => __( \'Post Comment\', \'pietergoosen\' ),
        \'format\'               => \'xhtml\',
        );
    return $defaults;
}

add_filter(\'comment_form_defaults\', \'pietergoosen_comment_form_fields\');

SO网友:The Quantum Physicist

Solution if submit button disappears and notes:

如果您丢失了表单的提交按钮,除了Pieter Goosen的答案之外,您还应该将这些行添加到defaults 阵列参数列表:

\'submit_button\'        => \'<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />\',
\'submit_field\'         => \'<p class="form-submit">%1$s %2$s</p>\',
因此,我将用以下内容重写他的完整代码:

function my_comment_form_fields( $args = array(), $post_id = null ) 
{
if ( null === $post_id )
    $post_id = get_the_ID();
else
    $id = $post_id;

$commenter = wp_get_current_commenter();
$user = wp_get_current_user();
$user_identity = $user->exists() ? $user->display_name : \'\';

$args = wp_parse_args( $args );
if ( ! isset( $args[\'format\'] ) )
    $args[\'format\'] = current_theme_supports( \'html5\', \'comment-form\' ) ? \'html5\' : \'xhtml\';

$req      = get_option( \'require_name_email\' );
$aria_req = ( $req ? " aria-required=\'true\'" : \'\' );
$html5    = \'html5\' === $args[\'format\'];
$fields   =  array(
     \'author\' =>
\'<p class="comment-form-author"><label for="author">\' . __( \'Name\', \'domainreference\' ) . \'</label> \' .
( $req ? \'<span class="required">*</span>\' : \'\' ) .
\'<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\', \'domainreference\' ) . \'</label> \' .
( $req ? \'<span class="required">*</span>\' : \'\' ) .
\'<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\', \'domainreference\' ) . \'</label>\' .
\'<input id="url" name="url" type="text" value="\' . esc_attr( $commenter[\'comment_author_url\'] ) .
\'" size="30" /></p>\',
);

$required_text = sprintf( \' \' . __( \'Required fiels are marked %s\', \'pietergoosen\' ), \'<span class="required">*</span>\' );

$fields = apply_filters( \'comment_form_default_fields\', $fields );
$defaults = array(
    \'fields\'               => $fields,
    \'comment_field\'        => \'<p class="comment-form-comment"><label for="comment">\' . _x( \'Comment\', \'Comments field name\', \'pietergoosen\' ) . \'</label> <textarea id="comment" name="comment" placeholder="\'.__( \'*Required* Enter your comment here. Minimum 20 characters, please\', \'pietergoosen\' ).\'" cols="45" rows="8" aria-required="true"></textarea></p>\',
    \'must_log_in\'          => \'<p class="must-log-in">\' . sprintf( __( \'You must be <a href="%s">logged in</a> to post a comment.\', \'pietergoosen\' ), wp_login_url( apply_filters( \'the_permalink\', get_permalink( $post_id ) ) ) ) . \'</p>\',
    \'logged_in_as\'         => \'<p class="logged-in-as">\' . sprintf( __( \'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>\', \'pietergoosen\' ), get_edit_user_link(), $user_identity, wp_logout_url( apply_filters( \'the_permalink\', get_permalink( $post_id ) ) ) ) . \'</p>\',
    \'comment_notes_before\' => \'<p class="comment-notes">\' . __( \'Your email address will not be published.\', \'pietergoosen\' ) . ( $req ? $required_text : \'\' ) . \'</p>\',
    \'comment_notes_after\'  => \'<p class="form-allowed-tags">\' . sprintf( __( \'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s\', \'pietergoosen\' ), \' <code>\' . allowed_tags() . \'</code>\' ) . \'</p>\',
    \'id_form\'              => \'commentform\',
    \'id_submit\'            => \'submit\',
    \'title_reply\'          => __( \'Leave a Reply\', \'pietergoosen\' ),
    \'title_reply_to\'       => __( \'Leave a Reply to %s\', \'pietergoosen\' ),
    \'cancel_reply_link\'    => __( \'Cancel reply\', \'pietergoosen\' ),
    \'label_submit\'         => __( \'Post Comment\', \'pietergoosen\' ),
    \'format\'               => \'xhtml\',
    \'submit_button\'        => \'<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />\',
    \'submit_field\'         => \'<p class="form-submit">%1$s %2$s</p>\',
    );
return $defaults;
}

add_filter(\'comment_form_defaults\', \'my_comment_form_fields\');

Important notes:

请记住,关键字“pietergoosen”是您在主题的“style.css”中找到的“文本域”或主题名称。对我来说,先把任何东西放在随机的位置都是可行的,但最好用正确的方法。

2-您不必修改主主题的functions.php 让这一切顺利进行。这将导致您的工作在更新后消失。创建新的child theme. 创建子主题后,您所要做的就是创建一个名为functions.php, 从<?php, 然后写下表单的代码。请勿结束functions.php 以通用PHP代码结尾?>. 这就是wordpress的语法functions.php.

干杯

结束

相关推荐

如何在使用WP_USE_THEMES=FALSE时显示管理栏?

因此,我以“无主题”的方式使用WordPress,即不使用“博客”模板。我的站点文件位于根目录中(不在主题文件夹中),WordPress安装在自己的目录中/wordpress/ 目录“我的主题”存在的唯一目的是定制后端,包括重新标记和自定义帖子类型。(我基本上不使用插件和小部件,而是定制WordPress的实现)通过这种设置,当我的客户像往常一样查看前端页面时,有没有办法让管理栏显示出来?注意:我已尝试添加wp_head() 和wp_footer() 无济于事。我想这可能与我的自定义文件结构有关。