2

I've almost finished coding up a function that allows contributers when they are the post author to manage comments left on their own post (portfolio) couldnt find a plugin to do it so had to get myself dirty.

It will basically work like this:

1) User leaves a comment on post_authors portfolio.

2) Post_author is notified by email that they have a comment for moderation (this bit is handled by a plugin "notify-on-comments").

3) Post_author logs in and goes to his/her portfolio page and in the comments are two links one for "delete" and one for "approve" comment.

Now i can get the delete to work on already published comments, my problem is that i am wanting to show the unpublished comment along with the published comments (dont want folks having access to wp-admin dashboard to moderate comments, i want it all done on the front end),

Does anyone know how i can do this part of showing the un-approved comment at front-end to contributers?

Once completed i will be more than happy to share the code and credits should anyone else need it.

Regards and thanks in advance

1
  • Just add 'edit_published_posts' capability to the user role.
    – Manu
    Apr 23, 2020 at 18:41

1 Answer 1

5

Easy:

function show_portfolio_comments( $post_ID ) 
{
    // NOT approved
    $comments_unapproved = get_comments( array( 'status' => 'hold', 'post_id' => $post_ID ) );
    foreach ( $comments_unapproved as $comments) 
    {
      if ( current_user_can( 'edit_published_posts' ) // maybe you'll have to switch to some other cap 
      {
      ?>
      <div class="comment">
         <h4>Unapproved Comments on your portfolio</h4>
         <div class="comment-author"><?php echo $comment->comment_author; ?></div>
         <div class="comment-content"><?php echo $comment->comment_content; ?></div>
      </div>
      <?php
      } // endif; - current_user_can( 'edit_published_posts' )
    }

    // ALREADY approved
    $comments_approved = get_comments( array( 'status' => 'approve', 'post_id' => $post_ID ) );
    foreach ( $comments_approved as $comments) 
    {
      ?>
      <div class="comment">
      <?php if ( current_user_can( 'edit_published_post' ) { ?>
         <h4>Approved Comments on your portfolio</h4>
      <?php }  // endif; - current_user_can( 'edit_published_posts' ) ?>
         <div class="comment-author"><?php echo $comment->comment_author; ?></div>
         <div class="comment-content"><?php echo $comment->comment_content; ?></div>
      </div>
      <?php
    }
}

Template Tag:

// Use it in your template like this & don't forget to push the post ID into it:
$post_ID = $GLOBALS['post']->ID;
// or:
global $post;
$post_ID = $post->ID;
// or:
$post_ID = get_the_ID();
// or:
$post_ID = get_queried_object_id();

show_portfolio_comments( $post_ID );
4
  • Hi there @Kaiser, thanks for taking the time out with this code, i can see from what you have done that it does what i require. The only issue is that i cannot get the code to run without erroring, i have used a portion of the code and it worked perfectly, but when i use the code either whole or just the un-approved portion i have an issue with the curly braces, i tried allsorts to get it to work but it wont have it. A good point you made about the "capability" in my case it needs to be edit_published_posts
    – MartinJJ
    Mar 22, 2011 at 7:59
  • @Martin: Glad it worked... so far, but you could be a little more specific: what error message and what about the curly braces? what did you try and how? Just update your answer, so i can take a look.
    – kaiser
    Mar 22, 2011 at 8:42
  • all is good now, the problem was the 2 if statements were both missing a closing bracket, which for the life of me i missed for ages. At this point now everything is working at 99% the problem im having is all unapproved comments are showing on all posts rather than on the post they are assigned to, so im summizing i need to find a way to fix these by post_id, many thanks for your help.
    – MartinJJ
    Mar 22, 2011 at 18:37
  • @Martin: See the updated answer. There where no missing closing curly brackets (marked them with a comment). I also updated the capability and the $post->ID. It should work as you'd expect. I also wrapped it into a function, that you can use as template tag. Just place the function into your functions.php file.
    – kaiser
    Mar 22, 2011 at 19:09

Your Answer

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

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