• So I’m in the process of making a website that requires a login to post, but all posts are anonymous. A feature I’d like to implement is to allow users to track their own comments and jump to their posts, to see if anyone has responded recently. I’ve found the below way of doing it, which works okay except for the fact that someone can just enter another user’s username to see their comment history. Since a lot of this website is predicated on no one knowing who posts what, this is a huge flaw given the mission of my site. Is there any way to modify the below code such that, when I place it in a page, it’ll get the comment history only for the person currently viewing the page, as opposed to a static given user?

    <?
    if(get_query_var('author_name')) :
    $curauth = get_userdatabylogin(get_query_var('author_name'));
    else :
    $curauth = get_userdata(get_query_var('author'));
    endif;
    $querystr = "
        SELECT comment_ID, comment_post_ID, post_title, comment_content
        FROM $wpdb->comments, $wpdb->posts
        WHERE user_id = $curauth->ID
        AND comment_post_id = ID
        AND comment_approved = 1
        ORDER BY comment_ID DESC
     ";
    
     $comments_array = $wpdb->get_results($querystr, OBJECT);
    
    if ($comments_array): ?>
    <h2>Your Recent Posts</h2>
    <ul>
    <? foreach ($comments_array as $comment):
    	setup_postdata($comment);
    	echo "<li><a href='". get_bloginfo('url') ."/?p=".$comment->comment_post_ID."'>Comment on ". $comment->post_title. "</a><br />". $comment->comment_content . "</li>";
    endforeach; ?>
    </ul>
    <? endif; ?>
Viewing 15 replies - 1 through 15 (of 16 total)
  • You could simply modify your query to get recent posts by currently logged in user :

    <?php
    
    if( is_user_logged_in() ) :
    	global $current_user;
    	get_currentuserinfo();
    
    	$querystr = "
    		SELECT comment_ID, comment_post_ID, post_title, comment_content
    		FROM $wpdb->comments, $wpdb->posts
    		WHERE user_id = $current_user->ID
    		AND comment_post_id = ID
    		AND comment_approved = 1
    		ORDER BY comment_ID DESC
    	 ";
    
    	$comments_array = $wpdb->get_results($querystr, OBJECT);
    
    	if( $comments_array ): ?>
    <h2>Your Recent Posts</h2>
    <ul>
    <?php
    		foreach ( $comments_array as $comment ):
    			setup_postdata( $comment );
    			echo "<li><a href='". get_bloginfo( 'url' ) ."/?p=".$comment->comment_post_ID."'>Comment on ". $comment->post_title. "</a><br />". $comment->comment_content . "</li>";
    		endforeach; ?>
    </ul><?php
    	endif;
    endif;
    ?>

    Thread Starter quiksilver189

    (@quiksilver189)

    That’s precisely what I’d like to do!

    Now, in terms of implementation, how would I put this on a page such that it’d work for anyone who’s logged in and viewing that page? Or at least, what’s the easiest way?

    The easiest way is to have a custom page handle this.

    1. Go to your theme folder
    2. Create a new file, say recent_comment.php
    3. Put the above code into this file, preceded by :
      <?php
      /*
      Template Name: User's Recent Comment
      */
      ?>
    4. In WP’s admin panel, create a new page and select “User’s Recent Comment” when choosing a page template (should be a dropdown menu on the right
    5. voila
    Thread Starter quiksilver189

    (@quiksilver189)

    Thank you so much! That’s perfect, you’ve been unbelievably helpful.

    My last, last question, and this one might be a bit harder. So I have my comments set up into pages of 10 comments each, and with this history page, ideally the links of each comment post would jump straight to the exact spot on the page, on the correct page of comments. So if I posted the 22nd comment on a post, clicking the link would bring me to the second page of the comments, a bit down the page to view my exact comment. How might I alter this template to adjust the links in that way?

    Well, this one isn’t harder, but this one could depend on your theme, so no certain answer here.

    Take a look at a link to a comment on your blog. You should see an anchor at its end, by example : #comment-672
    (Same on this forum : click on the # on left side of your last post to see an anchor.)
    All you have to do is modify one line of your code this way :
    echo "<li><a href='". get_bloginfo( 'url' ) ."/?p=".$comment->comment_post_ID."#comment-".$comment->comment_ID."'>Comment on ". $comment->post_title. "</a><br />". $comment->comment_content . "</li>";

    Of course, it won’t work if your theme doesn’t set these anchor when displaying comments on a post…

    Thread Starter quiksilver189

    (@quiksilver189)

    It looks like that works perfectly for any comment that’s on the first page of comments, but anything after page 1 (thus comment 10) it just links straight to the first page of comments. Any ideas? I’m using WP-Paginate if that helps at all.

    I must say again, you are incredibly helpful. Thank you so much!

    I’ve never used WP-Paginate…

    Could you please show what the URL of comments page 2 looks like ?
    Chances are you only need to add a parameter la &page=2 in the URL to access it, and I can help you include this in the above code…

    Thread Starter quiksilver189

    (@quiksilver189)

    http://www.homepageurl.com/archives/209/comment-page-2#comments

    whereas when it’s page one it’s just:

    http://www.homepageurl.com/archives/209#comments

    the 209 is just the blog post number.

    Something like this should work :

    <?php
    
    if( is_user_logged_in() ) :
    	global $current_user;
    	get_currentuserinfo();
    
    	$querystr = "
    		SELECT comment_ID, comment_post_ID, post_title, comment_content
    		FROM $wpdb->comments, $wpdb->posts
    		WHERE user_id = $current_user->ID
    		AND comment_post_id = ID
    		AND comment_approved = 1
    		ORDER BY comment_ID DESC
    	 ";
    
    	$comments_array = $wpdb->get_results($querystr, OBJECT);
    
    	if( $comments_array ): ?>
    <h2>Your Recent Posts</h2>
    <ul>
    <?php
    		$count = 0;
    		$page = 0;
    		foreach ( $comments_array as $comment ):
    			if($count++ % 0) $page++;
    			setup_postdata( $comment );
    			echo "<li><a href='". get_bloginfo( 'url' ) ."/?p=".$comment->comment_post_ID.($page > 1 ? '-page-'.$page : '')."#comment-".$comment->comment_ID."'>Comment on ". $comment->post_title. "</a><br />". $comment->comment_content . "</li>";
    		endforeach; ?>
    </ul><?php
    	endif;
    endif;
    ?>

    Thread Starter quiksilver189

    (@quiksilver189)

    Tried it out, no dice. It looks like the links being generated for second-page comments aren’t following the $page > 1 conditional, so they look exactly like links to comments on page 1, but with a different post ID. They end up just linking to the first comment on the first page then. I’m wondering if the stored post ID isn’t storing anything about what page it’s actually on?

    Also, if it helps, here’s what the link to a comment on another page looks like (since you’re linked to it after posting it):

    http://www.homepageurl.com/archives/209/comment-page-2#comment-89

    Or maybe I’m overthinking it. Thoughts?

    Right, I misunderstood the whole idea. I thought your where looping (i.e. foreach, here) through all comments for a specific post, I forgot it was all comments from an author.

    Anyway, after a short research, I found an easier way to do this :
    get_comment_link()

    <?php
    
    if( is_user_logged_in() ) :
    	global $current_user;
    	get_currentuserinfo();
    
    	$querystr = "
    		SELECT comment_ID, comment_post_ID, post_title, comment_content
    		FROM $wpdb->comments, $wpdb->posts
    		WHERE user_id = $current_user->ID
    		AND comment_post_id = ID
    		AND comment_approved = 1
    		ORDER BY comment_ID DESC
    	 ";
    
    	$comments_array = $wpdb->get_results($querystr, OBJECT);
    
    	if( $comments_array ): ?>
    <h2>Your Recent Posts</h2>
    <ul>
    <?php
    		foreach ( $comments_array as $comment ):
    			setup_postdata( $comment );
    			echo "<li><a href='".get_comment_link($comment->comment_ID)."'>Comment on ". $comment->post_title. "</a><br />". $comment->comment_content . "</li>";
    		endforeach; ?>
    </ul><?php
    	endif;
    endif;
    ?>
    Thread Starter quiksilver189

    (@quiksilver189)

    The links seem to be forming correctly now (with the pages in the generated link), but it seems that regardless of what page the actual comment is on, they all link to page 1 of the comments.

    For example:

    http://www.homepageurl.com/archives/209/comment-page-1#comment-90

    Despite that comment being on page 2

    Could be a problem caused by WP-Paginate, but since I don’t know this plugin and don’t want to test it, I can’t help you any further, sorry.

    Thread Starter quiksilver189

    (@quiksilver189)

    Actually I thought about it, and WP-paginate is unlikely to be the problem. It uses the regular structure for pages and interlinking between pages, but just creates more links to non-adjacent pages for ease of navigation. I think for this scenario we can ignore that it exists.

    http://codex.wordpress.org/Function_Reference/get_comment_link

    I’m looking around here and it seems that we can set the $per_page argument to maybe tell the get_comment_link when a post will be on another page. Since it defaults to 0 and it isn’t set in the code you gave me last, maybe that’s why it’s only linking to the first page of comments? Again, I’m totally ignorant of how php works, so maybe this is all gibberish. Or maybe we can use a bit of the code you gave earlier to tell it what to use for the $page argument?

    Yep, we could try this.
    But to set this $per_page argument, we need to know how many comments per page you have. πŸ˜‰

    [edit]
    Anyway :
    replace : get_comment_link($comment->comment_ID)
    by : get_comment_link($comment->comment_ID, array(‘per_page’ => 123))
    if 123 comments per page.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Creating a Comment History?’ is closed to new replies.