• uwew

    (@uwew)


    Hey there,

    I am sorry if this has been asked a million of times, but I just could not find the answer to a burning question of mine: I am tasked with a “special WordPress site” that utilizes page comments to review a piece of video. However, the main objective is that the comments should *not* be visible to anybody besides the Author and/or the Site Editor. I found a few examples to customize the query, but I am not sure where exactly to “plug them in” (i.e. which php script – core or theme)> Moreover, the examples I found did not do the trick:(.

    Any help would be greatly appreciated. The site is running the latest version of WordPress (3.7.1), very few plugins and utilizes the Woo Themes “Canvas” theme.

    Hope to hear from anybody, Thank you in advance,

    – Uwe Willenbacher

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Do the author and editor need to see comments on the page, or is viewing via the comments table admin screen adequate? There’s a solution either way. I don’t know the specifics of your theme, but most themes call wp_list_comments() on the comments template to display comments. If admin screen viewing is adequate, simply removing this function call from the comments template will do the job.

    If they really need to see the comments on the page, the wp_list_comments() is typically passed a custom callback by the theme. In the default themes (twentythirteen etc.), this callback is pluggable. Simply re-defining the function in a child theme will cause the child version to be used. One way or the other, the callback can be modified to only display comments to the author or editor, though I would suggest displaying comments to the comment author as well to avoid duplicate comment post attempts.

    It would be more efficient to alter the comments query instead of the results. I recently spent some time trying to identify the proper hook to do this and was unsuccessful. In that case I ended up manipulating the results instead of the query. Unless someone else pipes up with a solution, I suggest you focus on the results as well.

    Thread Starter uwew

    (@uwew)

    Hey there,

    The Author and Editor need to see the comments on the page. I need to avoid redirecting the end user to the admin screen at any cost. It would just confuse them and they may think something is wrong (I am not kidding:)

    I thought about altering the comments query but I have to be honest: I am not that verse with db queries. Guess I am what you’d call a “google programmer” I am looking for samples and forge them to work for me…

    Having that said, I would greatly appreciate some “hand holding’…

    Thread Starter uwew

    (@uwew)

    Douh! I think I now understand – should have turned on the gray cell’s before I reply;) I got it: All I really would have to do is check the current user vs. the Autor of the comment and if they are identical, or if we are an Editor, we can see the comment.

    One problem I am running into though: I would like to allow users to delete their own comments and in order to do that one has to be an “Editor”…right?

    Again, any help is greatly appreaciated

    Moderator bcworkz

    (@bcworkz)

    ?? I’ve also needed to do a few Doh! posts myself from not reading carefully enough the first time around. Sorry for the late reply, I was out of communication for a while.

    Yes, you’ve got the right idea, both on comment visibility and deleting (deleting as the default condition anyway, the functionality can be altered). Ideally a comment author front end delete functionality is best done using an AJAX approach which is a bit involved if you have never done that sort of thing in WordPress. As it happens, I just helped another forum member with the same need, also with limited PHP and javascript skills.

    I came up with some simple, non-AJAX code that helped him, I’ll share it here as I couldn’t subject anyone to the original thread. (The one on this sub-form with many dozens of posts).

    The main delete function goes on your child theme’s functions.php.:

    // Front end comment delete
    add_action('init', 'bcw_front_delete');
    function bcw_front_delete() {
    	if (array_key_exists('bcw_task', $_GET)) {
    		if ('del' == $_GET['bcw_task']) {
    			check_admin_referer('del-comment');		//only accept valid requests
    			global $wpdb;
    			if ( current_user_can('moderate_comments') || $_GET['user_id'] == get_current_user_id()) {
    				wp_set_comment_status( $_GET['com_id'], 'trash');
    				// delete all associated meta
    				$wpdb->delete( $wpdb->commentmeta, array('comment_id' => $_GET['com_id']));
    			}
    		}
    	}
    	return;
    }

    You can delete the meta data line for default comments, it works well if you’ve added other comment fields that are stored in comment meta. You will also need the following hook in functions.php to get the comment counts coordinated with what’s seen. (If your theme displays comment counts in the same way as the default themes that is):

    //adjust comment count for single page visibility
    add_filter('get_comments_number', 'bcw_adjust_comment_count');
    function bcw_adjust_comment_count( $count ) {
    	global $wp_query;
    	if ( !is_single()) return $count;
    	return $wp_query->comment_count;
    }

    To add a delete link to the comment display that sends the proper data to the first above function, add the following code to the function that displays each comment. This is usually a callback to wp_list_comments(). Check your theme’s comments.php file to see how comment display is handled.

    $user_id = get_current_user_id();
    $nonce = wp_create_nonce('del-comment');
    $link = get_permalink();
    if ( current_user_can('moderate_comments') || $comment->user_id == $user_id ) {
    	echo " | <a href=\"$link?bcw_task=del&com_id={$comment->comment_ID}&user_id=$user_id&_wpnonce=$nonce\" title=\"Move immediately to trash (no confirmation)\">Delete</a>";
    }

    It sounds like you figured out the visibility thing, but FYI, here is how I changed the query results to limit visibility (also goes on functions.php):

    //Alter comment results if not admin or editor to only have current user's comments
    add_filter('comments_array', 'bcw_current_user_comments');
    function bcw_current_user_comments( $comments ) {
    	if ( ! current_user_can('moderate_comments')) {
    		$comments = array_merge( array_filter( $comments, function( $comment ) {
    			if ( $comment->user_id == get_current_user_id() ) return true;
    			return;
    		}));
    	}
    	return $comments;
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Page comments’ is closed to new replies.