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 : ?>
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

