0

I want to add an inline word(s) after comments of post author's name. Suppose, my name is Tiger and i am the post author, so Post author will be printed after my name every time when i add a comment on the post.

Like this picture

I don't want to edit comment.php file. I want custom functions for functions.php

1
  • Please, see my updated question.
    – akarim
    Mar 13 '16 at 12:44
2

Just wanted to mention some alternatives:

Alternative #1

You say you don't want to edit the comments.php file in the current theme directory. If we need to change how the wp_list_comments() works, we can always modify it through the wp_list_comments_args filter. For example to add our own callback:

add_filter( 'wp_list_comments_args', function( $r )
{
    if( function_exists( 'wpse_comment_callback' ) )
        $r['callback'] = 'wpse_comment_callback';
    return $r;
} );

where the wpse_comment_callback() callback contains the customization of your comment's layout.

Alternative #2

If we take a look at e.g. the Twenty Sixteen theme, we will see how the .bypostauthor class is used to mark the post comment author from the stylesheet:

.bypostauthor > article .fn:after {
        content: "\f304";
        left: 3px;
        position: relative;
        top: 5px;
}

where you could adjust the content attribute to your needs. This might not be the workaround for you, if you need to style it or use words with translations.

2
  • +1 this is exactly the kind of thing that if possible should be done in CSS as it is a decoration and not a content. The downside, is that it is less readable - harder to find. Mar 13 '16 at 16:59
  • thanks, this would at least be handy for font-icons ;-) Let's see if any additional styling is needed by the OP, since I'm not entirely sure what he's after here @MarkKaplun
    – birgire
    Mar 13 '16 at 17:07
1

You can make use of the get_comment_author filter to adjust the text displayed as comment author name according to your needs. All you need to do is to check who the post author is and then check that against the comment author.

The complete comment object is passed by reference as third parameter to the filter, we can access that object to get the comment author which we can compare to the post_author of the post object

add_filter( 'get_comment_author', function (  $author, $comment_ID, $comment )
{
    // Get the current post object
    $post = get_post();

    // Check if the comment author is the post author, if not, bail
    if ( $post->post_author !== $comment->user_id )
        return $author;

    // The user ids are the same, lets append our extra text
    $author = $author . ' Post author';

    return $author;
}, 10, 3 );

You can adjust and add styling as needed

EDIT - from comments

As pointed out by @birgire, the get_comment_class() sets the .bypostauthor class for the post author comments.

// For comment authors who are the author of the post
if ( $post = get_post($post_id) ) {
    if ( $comment->user_id === $post->post_author ) {
        $classes[] = 'bypostauthor';
    }
}

We can also use this to check for comments by the post author. Just a not, it may not be very reliable as it can be overriden by themes or plugins

add_filter( 'get_comment_author', function (  $author )
{
    if( !in_array( 'bypostauthor', get_comment_class() ) )
        return $author;

    // The user ids are the same, lets append our extra text
    $author = $author . ' Post author';

    return $author;
});
5
  • I wonder if it would work to check for the .bypostauthor class with in_array( 'bypostauthor', get_comment_class() ) within the comment author filter ?
    – birgire
    Mar 13 '16 at 16:24
  • I actually quickly checked your answer were you used the bypostauthor class. Mar 13 '16 at 16:26
  • ok, but that class should be generated in get_comment_class() .
    – birgire
    Mar 13 '16 at 16:42
  • @birgire sorry missed it, I searched for .bypostauthor and not bypostauthor, so my editor did not pick it up. So stupid, I should have removed the exact search feature Mar 13 '16 at 16:48
  • 1
    the comment_class() class is called within Walker_Comment so it should be mostly theme independent, but that could be overridden with custom walkers or the comment_class filter. .. just read your updated comment ... yes I do that regularly too when searching ;-)
    – birgire
    Mar 13 '16 at 16:51
0

If I'm not getting it wrong, you are going to add some text to commenter's name if he is the post author, right?

You need to hook into get_comment_author. Check either the post author is the commenter. If yes, then return your custom text, otherwise return original text.

Use this code-

add_filter( 'get_comment_author', 'add_text_to_comment_author' );
function add_text_to_comment_author( $author ){
    global $post;
    if( get_comment( get_comment_ID() )->user_id == $post->post_author ){
        return "custom text here " . $author;
    }
    return $author;
}
2
  • i think you understand my problem but this code not working. I updated my question with screenshot what i wanted.
    – akarim
    Mar 13 '16 at 13:41
  • Only variables should be passed by reference Mar 13 '16 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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