Changing the Comment Fields using Filter (without success)

Am I doing this all wrong or should this be working? No matter what post I visit, it's thrown into an endless loop..

This is the code:

add_filter('comment_form_fields', 'comment_form_args');
function comment_form_args( $fields ) {

  if ( ! is_user_logged_in() ) {
    comment_form(array(
    'label_submit' = __('Submit for Approval'),
    'comment_notes_before' = '',
    'title_reply' = 'Need to ask something? Go ahead.',
    'title_reply_to' = 'Your reply to %s',
    'comment_field' = 'p class=comment-form-commentlabel for=comment' . _x( '', 'noun' ) . '/labelbr /textarea id=comment name=comment aria-required=true/textarea/p',
    'comment_notes_after' = 'pBe polite and specific. Spam will be deleted./p'
    )
  );
  } else {

    comment_form(array(
    'label_submit' = __('Submit'),
    'comment_notes_before' = '',
    'title_reply' = '',
    'title_reply_to' = 'Answer %s',
    'comment_field' = 'p class=comment-form-commentlabel for=comment' . _x( '', 'noun' ) . '/labelbr /textarea id=comment name=comment aria-required=true/textarea/p',
    'comment_notes_after' = ''
    )
  );

  }
}

Topic comment-form comments Wordpress

Category Web


Your code has 2 major issues:

  1. It's missing a return $fields; or a return value — Filter callbacks must always return something, which is normally the first parameter after it's filtered (regardless it's actually modified or not by your callback).

  2. Do not call comment_form() because comment_form_fields is fired in that function, hence you'll run into a never-ending loop, just like you've seen it yourself.

And looking at your code which calls comment_form(), I believe the hook you should be using is comment_form_defaults and not comment_form_fields.

So try the following instead:

// Use comment_form_defaults and not comment_form_fields.
add_filter( 'comment_form_defaults', 'comment_form_args' );

function comment_form_args( $args ) {
    if ( ! is_user_logged_in() ) {
        return array_merge( $args, array(
            'label_submit'         => __( 'Submit for Approval' ),
            'comment_notes_before' => '',
            'title_reply'          => 'Need to ask something? Go ahead.',
            'title_reply_to'       => 'Your reply to %s',
            'comment_field'        => '<p class="comment-form-comment"><label for="comment">' . _x( '', 'noun' ) . '</label><br /><textarea id="comment" name="comment" aria-required="true"></textarea></p>',
            'comment_notes_after'  => '<p>Be polite and specific. Spam will be deleted.</p>'
        ) );
    } else {
        return array_merge( $args, array(
            'label_submit'         => __( 'Submit' ),
            'comment_notes_before' => '',
            'title_reply'          => '',
            'title_reply_to'       => 'Answer %s',
            'comment_field'        => '<p class="comment-form-comment"><label for="comment">' . _x( '', 'noun' ) . '</label><br /><textarea id="comment" name="comment" aria-required="true"></textarea></p>',
            'comment_notes_after'  => ''
        ) );
    }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.