0

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-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 {

    comment_form(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' => ''
    )
  );

  }
}

1 Answer 1

0

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'  => ''
        ) );
    }
}
2
  • PS: If you did want to modify the comment fields like Name and Email, then you can use the fields arg to modify the fields, e.g. for the Name field, add $args['fields']['author'] = 'your code'; before the return line.
    – Sally CJ
    Jan 14 at 0:56
  • 1
    Thank you so much Sally! I appreciate you taking the time to answer and for providing some insight and understanding. Thanks. Jan 14 at 6:17

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.