Support » Plugins » Restricting users to seeing only comments on their posts in admin

  • Resolved dochara

    (@dochara)


    I want to restrict contributer and author users to seeing and manageing comments only on their own posts.

    I have tried the following filter with no success.

    //Manage Your Own Comments Only
        function myplugin_comments_thisuseronly( $wp_query ) {
    if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit-comments.php' ) !== false ) {
        if ( !current_user_can( 'edit_others_posts' ) ) {
            global $current_user;global $wpdb;
            $usersposts = $wpdb->get_results( $wpdb->prepare("SELECT ID FROM wp_posts WHERE post_author = %s", $current_user->id) );
            $wp_query->set('comment_post_ID', array($usersposts));
        }
    }
    }
    add_filter('parse_query', 'myplugin_comments_thisuseronly' );

    I also tried

    $wp_query->set('comment_post_ID__in', array($usersposts));

Viewing 4 replies - 1 through 4 (of 4 total)
  • I meant to come back and update this with a solution, though I can’t remember now where I found it.

    # ------------------------------------------------------------
    # Ensure that non-admins can see and manage only their own comments
    # ------------------------------------------------------------
    
    function myplugin_get_comment_list_by_user($clauses) {
    	if (is_admin()) {
    		global $user_ID, $wpdb;
    		$clauses['join'] = ", wp_posts";
    		$clauses['where'] .= " AND wp_posts.post_author = ".$user_ID." AND wp_comments.comment_post_ID = wp_posts.ID";
    	};
    	return $clauses;
    };
    // ensure that editors and admins can moderate everything
    if(!current_user_can('edit_others_posts')) {
    add_filter('comments_clauses', 'myplugin_get_comment_list_by_user');
    }

    Hopefully will save someone some time.

    thank you

    Thank YOU!!!

    MyWPorg

    (@mywordpressorg)

    Where do you put this code into?
    Which file?

    thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Restricting users to seeing only comments on their posts in admin’ is closed to new replies.