1

I am trying to remove Your email address will not be published. line from comment form in twentythirteen and twentyfourteen default themes.

I have tried this:

function alter_comment_form_fields($fields){
unset($fields['comment_notes_before']);
return $fields;
}
add_filter('comment_form_default_fields','alter_comment_form_fields');

and this:

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');

Nothing seems to work. I have also found this ticket https://core.trac.wordpress.org/ticket/14510 Would appreciate if someone can point me to some direction.

1

Yor code is totally wrong here. Your idea here is not bad. If you have a look at the default comment_form, you will see $fields consist of the following, author, email and URL.

That is why your code doesn't work, as comment_notes_before aren't in the $field field.

I personally think here that the proper way to go here is to copy the first section of the comment_form() to your theme, rename it and do all your changes in there, and just hooking it back to the proper filter.

My reasoning here is, firstly proper localization of the comment_form() within your theme. And secondly, I'm using a jquery based validation system, and for this to work properly, the way to go here is copying the first part of the comment_form(). This is tried and tested, you can see my question that I raised here on this particular subject.

So after all this said, here is what I'm currently using in my theme. You will need to do exactly the same. You just have to change want to need to what you want.

 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');
  • 1
    Thanks for the help. Instead of writing the whole comment form again I used this: <?php comment_form(array( 'comment_notes_before' => '<p class="comment-notes">' . __( 'No email or name required.' ) . ( $req ? $required_text : '' ) . '</p>' )); ?> – Noumaan Yaqoob Mar 27 '14 at 19:04
  • Thank you for this helpful answer. May I ask though, why is "pietergoosen" the second parameter in empty __() and sprintf functions. I know this is your name, but can I, like, put anything there? Could you please explain this? I'd be grateful. – The Quantum Physicist May 5 '15 at 8:50
  • @TheQuantumPhysicist My pleasure, glad it helped you. pietergoosen is the name of my theme. You cannot use anything in there, this value should correspond with the value of Text Domain as set in your theme main stylesheet header otherwise your translations would work – Pieter Goosen May 5 '15 at 9:10
  • @PieterGoosen Thank you for the response. Actually it has been working for a while with a random text there, but then after an update the submit button disappeared, and I started debugging, and it turns out it has nothing to do with it. In case the submit button disappears for anyone, the solution is adding those two parameters to the list above: '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>', – The Quantum Physicist May 5 '15 at 9:33
  • @TheQuantumPhysicist thanks for the info, but it is rather useless in comments. Most users don't read comments. You should add this as your own answer. Remember, when you add an answer, you should state since when the code stopped working and your code that solves this particular issue. Thanks :-) – Pieter Goosen May 5 '15 at 9:37
1

Solution if submit button disappears and notes:

In case you lose the submit button of your form, in addition to Pieter Goosen's answer, you should add those lines to the defaults array parameters list:

'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>',

So I'll rewrite his full code with this:

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:

1- Remember that the keyword "pietergoosen" is the "Text Domain" or the theme name that you find in "style.css" of your theme. It worked for me to put anything random firstly, but then it's better to use it the right way.

2- You don't have to modify the main theme's functions.php to make this work. This will cause your work to vanish after updates. It's good practice to create a new child theme. All you have to do after creating the child theme is create a new file called functions.php, and start it with <?php, and then write the code of the form. DO NOT END functions.php WITH THE COMMON PHP CODE ENDING ?>. This is simply the wordpress syntax for functions.php.

Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.