9

Some of my users have mentioned that my site is confusing for them. As this is the case, I would like to actually make links to Login/Register for my unregistered visitors to quickly do so if they would like to leave a comment.

Right now it just says "You must be logged in to post a comment." with no link to Login. Where can I edit this and change it to "You must Register or Login to post a comment." with links to them?

1 Answer 1

10

You can try to modify it with the comment_form_defaults filter:

/**
 * Modify the "must_log_in" string of the comment form.
 *
 * @see http://wordpress.stackexchange.com/a/170492/26350
 */
add_filter( 'comment_form_defaults', function( $fields ) {
    $fields['must_log_in'] = sprintf( 
        __( '<p class="must-log-in">
                 You must <a href="%s">Register</a> or 
                 <a href="%s">Login</a> to post a comment.</p>' 
        ),
        wp_registration_url(),
        wp_login_url( apply_filters( 'the_permalink', get_permalink() ) )   
    );
    return $fields;
});

where we use the wp_registration_url() and wp_login_url() core functions.

ps: the info on the comment_form_defaults filter seems to be missing from the Codex.

9
  • Thank you for the help. Sorry to be stupid but where do I put this, functions.php? Dec 7, 2014 at 13:12
  • How about this part of the code wp_login_url( get_permalink() ) ); Feb 7, 2015 at 9:22
  • yes, that should work as well. I just wanted to emulate how it's done in the core. @BradDalton
    – birgire
    Feb 7, 2015 at 13:36
  • Sure but why return apply_filters when modifying the default output for the text in a child theme? Feb 7, 2015 at 13:37
  • 1
    Here is the codex link for comment_form_defaults developer.wordpress.org/reference/hooks/comment_form_defaults
    – theKing
    Dec 28, 2021 at 12:43

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