0

I am trying to display the current user's ALL comments in a specific page and I'm using this code (uses a shortcode): https://blog.ashfame.com/2011/01/show-recent-comments-particular-user-wordpress/

And have modified it:

<?php
/*
Plugin Name: Show Recent Comments by a particular user
Plugin URI: http://blog.ashfame.com/?p=876
Description: Provides a shortcode which you can use to show recent comments by a particular user
Author: Ashfame
Author URI: http://blog.ashfame.com/
License: GPL
Usage: 
*/

add_shortcode ( 'show_recent_comments', 'show_recent_comments_handler' );

function show_recent_comments_handler( $atts, $content = null )
{
    extract( shortcode_atts( array( 
        "count" => 10,
        "pretty_permalink" => 0
        ), $atts ));

    $output = ''; // this holds the output
    
    if ( is_user_logged_in() )
    {
        global $current_user;
        get_currentuserinfo();

        $args = array(
            'user_id' => $current_user->ID,
            'number' => $count, // how many comments to retrieve
            'status' => 'approve'
            );

        $comments = get_comments( $args );
        if ( $comments )
        {
            $output.= "<ul>\n";
            foreach ( $comments as $c )
            {
            $output.= '<li>';
            if ( $pretty_permalink ) // uses a lot more queries (not recommended)
                $output.= '<a href="'.get_comment_link( $c->comment_ID ).'">';
            else
                $output.= '<a href="'.get_settings('siteurl').'/?p='.$c->comment_post_ID.'#comment-'.$c->comment_ID.'">';           
            $output.= $c->comment_content;
            $output.= ', Post Name: '.get_the_title($c->comment_post_ID);
            $output.= '</a>, Posted on: '. mysql2date('m/d/Y', $c->comment_date, $translate);
            $output.= "</li>\n";
            }
            $output.= '</ul>';
        }
    }
    else
    {
        $output.= "<h2>You should be logged in to see your comments. Make sense?</h2>";
        $output.= '<h2><a href="'.get_settings('siteurl').'/wp-login.php?redirect_to='.get_permalink().'">Login Now &rarr;</a></h2>';
    }
    return $output;
}
?>

It possible to display the comments like this:

  • Post title (title of the post where the user leaved comment)

    • comment excerpt (This is a comment I made on......)
    • comment date
  • Post title (title of the post where the user leaved comment)

    • comment excerpt (This is a comment I made on......)
    • comment date
  • Post title (title of the post where the user leaved comment)

    • comment excerpt (This is a comment I made on......)
    • comment date

Because it doesn't look good using my current code, see screenshot here: https://prnt.sc/xd8qmc

Is it also possible to paginate the comments?

2
  • You didn't include the screenshot. But it sounds like this is just a styling question, and you've got the WordPress-specific parts working?
    – Rup
    Commented Jan 21, 2021 at 12:06
  • Yes, the styling only. Here's the screenshot: prnt.sc/xd8qmc Commented Jan 21, 2021 at 12:08

1 Answer 1

0

add this snippet for according your needed:

add_shortcode ( 'show_recent_comments', 'show_recent_comments_handler' );

function show_recent_comments_handler( $atts, $content = null )
{
    extract( shortcode_atts( array( 
        "count" => 10,
        "pretty_permalink" => 0
        ), $atts ));

    $output = ''; // this holds the output
    
    if ( is_user_logged_in() )
    {
        global $current_user;
        get_currentuserinfo();

        $args = array(
            'user_id' => $current_user->ID,
            'number' => $count, // how many comments to retrieve
            'status' => 'approve'
            );

        $comments = get_comments( $args );
        if ( $comments )
        {
            $output.= "<ul>\n";
            foreach ( $comments as $c )
            {
            $output.= '<li>';

            $output.= 'Post Name: '.get_the_title($c->comment_post_ID);
            $output.= "<ul>";

            if ( $pretty_permalink ) // uses a lot more queries (not recommended)
                $output.= '<a href="'.get_comment_link( $c->comment_ID ).'">';
            else
                $output.= '<a href="'.get_settings('siteurl').'/?p='.$c->comment_post_ID.'#comment-'.$c->comment_ID.'">';        
            $output.='<li>'. $c->comment_content .'</li>';
            $output.= '</a><li> Posted on: '. mysql2date('m/d/Y', $c->comment_date, $translate).'</li>';
            $output.= '</ul>';
            $output.= "</li>\n";
            }
            $output.= '</ul>';
        }
    }
    else
    {
        $output.= "<h2>You should be logged in to see your comments. Make sense?</h2>";
        $output.= '<h2><a href="'.get_settings('siteurl').'/wp-login.php?redirect_to='.get_permalink().'">Login Now &rarr;</a></h2>';
    }
    return $output;
}
1
  • Thanks for the answer! Is it also possible to paginate the comments? Commented Jan 31, 2021 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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