• Resolved stellaa

    (@stellaa)


    Hello
    With this code, I have the user role next to the name in the comments section, but instead of displaying the administrator’s name, I want it to display the desired name that I want.
    What should I do?

    /** Add User Role to Comments. */
    if ( ! class_exists( 'Comment_Author_Role_Label' ) ) :
    class Comment_Author_Role_Label {
    public function __construct() {
    add_filter( 'get_comment_author', array( $this, 'get_comment_author_role' ), 10, 3 );
    add_filter( 'get_comment_author_link', array( $this, 'comment_author_role' ) );
    }
    function get_comment_author_role($author, $comment_id, $comment) {
    $authoremail = get_comment_author_email( $comment);
    if (email_exists($authoremail)) {
    $commet_user_role = get_user_by( 'email', $authoremail );
    $comment_user_role = $commet_user_role->roles[0];
    $this->comment_user_role = ' ' . ucfirst($comment_user_role) . '';
    } else {
    $this->comment_user_role = '';
    }
    return $author;
    }
    function comment_author_role($author) {
    return $author .= $this->comment_user_role;
    }
    }
    new Comment_Author_Role_Label;
    endif;
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi @stellaa

    I assume you want to change “Administrator” to something else – and not the name “peter”.
    In the code you’ve provided, it fetches the userrole here:

    $comment_user_role = $commet_user_role->roles[0];

    And then it sets the first letter to capital here (ucfirst):

    $this->comment_user_role = ' ' . ucfirst($comment_user_role) . '';

    If you want to change the administrator role so something else – let’s say you would like it to say “Admin” instead – then I would change the code to look something like this:

    /** Add User Role to Comments. */
    if ( ! class_exists( 'Comment_Author_Role_Label' ) ) :
    class Comment_Author_Role_Label {

    public function __construct() {
    add_filter( 'get_comment_author', array( $this, 'get_comment_author_role' ), 10, 3 );
    add_filter( 'get_comment_author_link', array( $this, 'comment_author_role' ) );
    }

    function get_comment_author_role($author, $comment_id, $comment) {
    $authoremail = get_comment_author_email( $comment );

    if (email_exists($authoremail)) {
    $comment_user = get_user_by( 'email', $authoremail );
    $comment_user_role = $comment_user->roles[0];

    switch ($comment_user_role) {
    case 'administrator':
    $this->comment_user_role = ' Admin';
    break;
    default:
    $this->comment_user_role = ' ' . ucfirst($comment_user_role);
    break;
    }
    } else {
    $this->comment_user_role = '';
    }

    return $author;
    }

    function comment_author_role($author) {
    return $author .= $this->comment_user_role;
    }

    }

    new Comment_Author_Role_Label;
    endif;

    Please note, I’m checking if the user is an “administrator”, before changing the name to “Admin” – you should also check for “Editor”, “Author”, “Contributor” and “Subscriber”. If you ONLY want to apply a custom label for the “administrator” userrole, then you can set the “default” (in the switch case) to an empty string, like this:

    default:
    $this->comment_user_role = '';
    break;

    Hope this helps ??

    /A

    • This reply was modified 3 months, 4 weeks ago by kuckovic. Reason: Corrected copy/pasted part of code
    Thread Starter stellaa

    (@stellaa)

    Hi
    Thank you so much , this code solved my problem.
    But I want to style the ‘ Admin ‘ name, what should I do?

    Like this :

    @stellaa

    I would suggest you look into some CSS.
    If the label does not have a CSS-class, you can add it in the code above.
    But from what I can see in the screenshot, you should have a different class, based on userroles.

    /A

    Thread Starter stellaa

    (@stellaa)

    It is , How can I style the admin?

    <header class="ast-comment-meta ast-row ast-comment-author vcard capitalize">
    <div class="ast-comment-cite-wrap">
    <cite><b class="fn">
    <a class="url" rel="ugc">Stella</a> Admin </b> <span class="ast-highlight-text ast-cmt-post-author"></span>
    </cite></div>

    @stellaa

    I’ve updated the code here below.
    I’ve wrapped the “Admin” word in <span>-tags with a class – you should be able to play around with that.

    <?php

    /** Add User Role to Comments. */
    if ( ! class_exists( 'Comment_Author_Role_Label' ) ) :
    class Comment_Author_Role_Label {

    public function __construct() {
    add_filter( 'get_comment_author', array( $this, 'get_comment_author_role' ), 10, 3 );
    add_filter( 'get_comment_author_link', array( $this, 'comment_author_role' ) );
    }

    function get_comment_author_role($author, $comment_id, $comment) {
    $authoremail = get_comment_author_email( $comment );

    if (email_exists($authoremail)) {
    $comment_user = get_user_by( 'email', $authoremail );
    $comment_user_role = $comment_user->roles[0];

    switch ($comment_user_role) {
    case 'administrator':
    $this->comment_user_role = ' <span class="comment-admin">Admin</span>';
    break;
    default:
    $this->comment_user_role = ' ' . ucfirst($comment_user_role);
    break;
    }
    } else {
    $this->comment_user_role = '';
    }

    return $author;
    }

    function comment_author_role($author) {
    return $author .= $this->comment_user_role;
    }

    }

    new Comment_Author_Role_Label;
    endif;
    Thread Starter stellaa

    (@stellaa)

    Is that working , Thank you so musch

    @stellaa

    I’m glad it worked ??
    Have a great day.

    /A

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.