Header Shadow Image


Users not able to comment on posts in WordPress even when logged in.

Users not able to post on WordPress even when logged in.  This code no longer works, because with passage of time, roughtly eh … (cough) … 12 years or so it’s about time to get off my a.. and update it.   Basically, the following line and user_ID no longer works to detect if you’re logged in or not.

    <?php if ( get_option(‘comment_registration’) && !$user_ID ) : ?>
        <p>You must be <a href=”<?php echo get_option(‘siteurl’); ?>/wp-login.php?redirect_to=<?php the_permalink(); ?>”>logged in</a> to post a comment.</p>
    <?php else : ?>

This code below now replaces the above code to check if user is logged in.  Appears functions ‘is_user_logged_in’, ‘wp_login_url’ and ‘get_permalink’ are new in the updated WordPress versions, so do not need to define these in the theme functions themselves:

    <?php if ( get_option(‘comment_registration’) && !is_user_logged_in() ) : ?>
        <p><?php printf(__(‘You must be <a href=”%s”>logged in</a> to post a comment.’, ‘kubrick’), wp_login_url( get_permalink() )); ?></p>
    <?php else : ?>

How did I find this?

So searching for this didn’t reveal very much that I could use for this particular issue.  There were some very insightful results to be fair, but mostly centered around digging in and debugging further, enabling DEBUG mode etc.  which they should.  I won’t go over that here.  After a good night’s rest it dawned upon me, to credit some posts online, that this works in newer themes.  So with that, I openeed up the default WordPress theme, which I just updated anyway, to see how things are being done.

And so the solution.  Before I pasted in the logic blindly to see if it works, I did end up checking the bolded functions above to see if and where they were defined.  I’m glad to say I didn’t find these in the theme, as I probably should not have, meaning they must have been introduced in the latest WordPress updates.  This was perfect because I didn’t have to go and define them or copy those over as well.

Anyway, hope this helps if you’re running into the same issue.

Cheers,
HTH

Subscribe
Notify of
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

     
  Copyright © 2003 - 2025 Tom Kacperski (microdevsys.com). All rights reserved.

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License

 

0
Would love your thoughts, please comment.x
()
x
The IT Development and Technology Mini Vault | MicroDevSys.com
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.