0

So here's what I'm trying to do. I'm working on a plugin and I want to check each comment and see if the author of that comment has a role of "administrator" or "editor". If they do, rather than display their user name and avatar, I would like to display the name of the website along with a company logo or something. I'm pretty new to WordPress development and am stuck on this. I can't figure out if there is a filter for this, or if I need to create a custom comments template. If someone could even just get me pointed in the right direction that would be great, because at this point, i'm not even sure where I should start. Thank you.

Where I am, My thought process:

<?php 
function anonymize_author(){
    global $post;

   //get the id of the comment author
    $author_id = $post->post_author;

    //get the userdata of comment author
    $author_info = get_userdata($author_id);

    //get the user roles of comment author
    $author_roles = $author_info->roles;

    //Array of roles to check against
    $roles_to_check = ["editor", "administrator"];

    //see if user has a role in my $roles_to_check array
    $results = array_intersect($roles_to_check, $author_roles);

    if(!empty($results)){
        //the user has roles of either "editor" or "administrator"
        //load custom comments page?
        //I need to display the author name as the site name
        //and the avatar as the site logo
    }else{
        //Just a regular user, load the Wordpress Default comments
    }
}

add_filter('some_filter_here', 'anonymize_author');
?>
1

You seem already to have an idea about how to use filters, so I won't provide an example here. Expanding on David Lee's answer, you can probably use get_avatar and get_comment_author_link filters for the main purpose you describe, and in your function or functions test whether the commenter is a registered user with the right accounts levels.

get_avatar makes the latter part easy, since it returns commenter ID or email. The linked get_avatar Codex entry provides a good example for getting you started. You could also just try out the filters, and see if they produce the expected results: If your simple get_avatar filter function replaces the avatar, then you'll know your theme is using get_avatar, and you can proceed from there. Ditto for get_comment_author_link.

Otherwise, unless you're going to replace your theme's commenting system entirely, there won't be a universal solution, because comment templates do differ: Just replacing the avatar with a different image using the get_avatar filter might work in 90%+ of WordPress themes, but fail in yours.

If it fails, there are a lot of tutorials and examples around for customizing comments. One way to proceed, and get an idea as to how far you're going to have to go if you encounter difficulties, would be first to check your theme's (or its parent theme's) comments.php template file, and make sure it's using wp_list_comments(), and, if so, how (especially whether it uses a 'walker' argument). It's even possible that you're already using a plugin that hijacks the standard comment form and replaces it with one of its own. In either case, however, you still might be able to use the various standard comment filters, since many custom commenting systems will still employ standard functions.

1

If you are searching to modify the listing of comments, check wp_list_comments() just like the nav menu you can modify it using a walker:

wp_list_comments( array(
    'walker' => new Walker_Comment()
) );

there is an example in that page, i think you want to modify this part:

<div class="comment-author vcard"><?php 
     if ( $args['avatar_size'] != 0 ) {
        echo get_avatar( $comment, $args['avatar_size'] ); 
     } 
     printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link() ); ?>
</div>

if you want to modify the comment form too, check comment_form()

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.