Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to remove published comments so they're hidden for logged out users?

share|improve this question
1  
You can wrap the comment display code in is_user_logged_in() function. codex.wordpress.org/Function_Reference/is_user_logged_in –  Vinod Dalvi Apr 19 '13 at 4:56
add comment

3 Answers

Based on an older article, this is how I would do it:

<?php # -*- coding: utf-8 -*-
/* Plugin Name: Show comments after login */

if ( ! function_exists( 'wpse_96406_comments_after_login' ) )
{
    add_filter( 'comments_template', 'wpse_96406_comments_after_login', 11 );

    function wpse_96406_comments_after_login( $comments_template )
    {
        return is_user_logged_in() ? $comments_template : __FILE__;
    }
}

Note: This must be a stand-alone plugin; do not put this code into a theme.

But you can also search for all occurrences of comments_template() in your theme and wrap those into a conditional.

share|improve this answer
    
Just a note: if you do this, the above should be the only thing in a file. comments_template will just blindly require it -- probably don't want anything else going on there. –  chrisguitarguy Apr 19 '13 at 5:59
1  
@chrisguitarguy I added a warning. :) –  toscho Apr 19 '13 at 6:02
add comment

I would recommend looking for all occurrences of comments_template in your theme and wrapping them in a is_user_logged_in condiational.

Example:

<?php
if (is_user_logged_in()) {
    comments_template();
}

Alternatively, you can open up your theme's comments.php (or the equivalent, it's usually comments.php) and add something like this at the very top:

<?php
if (!is_user_logged_in()) {
    return;
}

PHP let's you return out of files early, so you can "bail" from an include like the above. If you need to remove comments on ALL post types for non-logged in users, the second option is likely the best. If you need it conditionally on some post types, the first is the best.

The best solution, however, is to use your own filters. Rather than calling is_user_logged_in directly, call apply_filters with a unique name and return try by default.

<?php
// somewhere before all the other stuff in comments.php
if (!apply_filters('wpse96406_show_comments', true)) {
    return;
}

Then in functions.php, hook into your own filter and modify how it behaves.

<?php
add_filter('wpse96406_show_comments', 'is_user_logged_in');

Why do this? Much more flexible.

Want to show comments on certain posts? The first two options above don't let you do that (without some refactoring). Using your own filter makes things much more extensible for your future self -- or for end users if you're releasing the theme.

share|improve this answer
add comment

Thanks. I resolved this by writing the custom function myself based on removing the comments form from Genesis.

share|improve this answer
1  
Please use just one account. –  toscho Apr 19 '13 at 7:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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