Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] My Comments Tab

  • @bigkahunaburger

    Participant

    Does anyone know of a plugin that can display a list of a users comments? I’m trying to create a profile tab that works like the forums “my topics” tab but with blog comments instead.

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • @danbp

    Participant

    hi @bigkahunaburger,

    Give this a try (goes to bp-custom.php or child-theme functions.php)

    function bpfr_get_user_comments_on_profile() {
    
    // number of comments to show per page
    define( 'DEFAULT_COMMENTS_PER_PAGE', 3 );
    
    $page = (get_query_var('page')) ? get_query_var('page') : 1;
    
    $limit = DEFAULT_COMMENTS_PER_PAGE;
    
    $offset = ($page * $limit) - $limit;
    
    $param = array(
    	'user_id' 	=> bp_displayed_user_id(),
    	'status'	=> 'approve',
    	'offset'	=> $offset,
    	'number'	=> $limit
    );
    
    $total_comments = get_comments(array(
    	'orderby' => 'comment_date',
    	'order'   => 'ASC',
    	'status'  => 'approve',
    	'parent'  => 0
    ));
    
    $pages = ceil(count($total_comments)/DEFAULT_COMMENTS_PER_PAGE);
    $comments = get_comments( $param );
    
    $args = array(
    	'base'         => @add_query_arg('page','%#%'),
    	'format'       => '?page=%#%',
    	'total'        => $pages,
    	'current'      => $page,
    	'show_all'     => false,
    	'mid_size'     => 4,
    	'prev_next'    => true,
    	'prev_text'    => __('« backward'),
    	'next_text'    => __('onward »'),
    	'type'         => 'plain'
    );
    
    // Output pagination
     echo paginate_links( $args );
    ?>
    
    <ul>
    <?php
    
    	foreach($comments as $comment) {
    
    	// short version
    	//echo '<li>'. ($comment->comment_content .'&nbsp;<a href="'. esc_url( get_permalink( $comment->comment_post_ID ) ) ).'">View post</a></li>';
    	
    	// long version
    		echo '<li>'. ($comment->comment_content .'&nbsp;<a href="'. esc_url( get_permalink( $comment->comment_post_ID ) ) ).'">'. get_the_title( $comment->comment_post_ID ) .'</a> Comment: '. get_comment_date( '', $comment ) .'</li>';
    	}
    ?>
    </ul>
    <?php
    
    }
    add_action ( 'my_profile_comment', 'bpfr_get_user_comments_on_profile' );
    
    function bpfr_comment_profile_setup_nav() {
    global $bp;
    
    	$parent_slug = 'comments';
    	$child_slug = 'comments_sub';	
    	
    	// Add nav item
    	bp_core_new_nav_item( array(
    	'name' => __( 'My comments' ),
    	'slug' => $parent_slug,
    	'screen_function' => 'bpfr_profile_comment_screen',
    	'position' => 40,
    	'default_subnav_slug' => $child_slug 
    	) );
    	
    	//Add subnav item 	 
    	bp_core_new_subnav_item( array( 
    	'name' => __( 'My latest comments' ), // as content title
    	'slug' => $child_slug, 
    	'parent_url' => $bp->loggedin_user->domain . $parent_slug.'/', 
    	'parent_slug' => $parent_slug, 
    	'screen_function' => 'bpfr_profile_comment_screen'
    	) );
    }
    
    		function bpfr_profile_comment_screen() {	
    			add_action( 'bp_template_content', 'bpfr_profile_comment_screen_content' );
    			bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
    		}
    
    		function bpfr_profile_comment_screen_content() {
    			do_action( my_profile_comment );
    		}
    
    add_action( 'bp_setup_nav', 'bpfr_comment_profile_setup_nav' );

    Reference: https://codex.wordpress.org/Function_Reference/get_comments

    To do the same for user’s post, see here:
    https://buddypress.org/support/topic/creating-a-custom-tab-that-takes-in-the-info-from-wp-recent-posts/

    @bigkahunaburger

    Participant

    @danbp That worked. Thanks!

    Is there a way to add pagination to it?

    @danbp

    Participant

    @bigkahunaburger,

    i completed the snippet so it comes with pagination.

    Here a great tutorial about pagination.

    @bigkahunaburger

    Participant

    Thanks that worked great!

    @danbp

    Participant

    @bigkahunaburger You’re welcome ! 😉

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] My Comments Tab’ is closed to new replies.
Skip to toolbar