Search code examples
wordpressblogsauthor

How to add verified badge in front of author name across wordpress blog


Good Day, I have been trying to add verification badge to WordPress users but I no success. I tried using administrator to do the testing by checking if user has role administrator and trying to enter code here update_user_meta (display_name) but I wasn't able to add icons to the display name.

I have as well tried creating a custom field in user profile called verify_user (text field) .... In which I entered the value "verified" and saved ... I have been searching for hooks to use but haven't seen one. I'm not sure which hook/filter to use here

Please is there any solution to adding this verification icon to author's display name in which when the word "verified" is entered into the custom field created in user profile or any other approach available (e.g if user has specific role). I don't mind if the little structure I wrote above would be changed.

Thanks


Solution

  • I was able to get a solution which worked exactly as i wanted. For anyone who might have same task to tackle;

    function add_verification_bagdge_to_authors($display_name) {
        global $authordata;
    
        if (!is_object($authordata))
            return $display_name;
    
        $icon_roles = array(
            'administrator',
            'verified_author',
        );
    
        $found_role = false;
        foreach ($authordata->roles as $role) {
            if (in_array(strtolower($role), $icon_roles)) {
                $found_role = true;
                break;
            }
        }
    
        if (!$found_role)
            return $display_name;
    
        $result = sprintf('%s <i title="%s" class="fa fa-check-circle"></i>',
            $display_name,
            __('This is a Verified Author', 'text-domain-here')
        );
    
        return $result;
    }
    add_filter( 'the_author', 'add_verification_bagdge_to_authors' );
    

    The way i was able to tackle this was to create a user role called Verified Author (using add_role() function from Wordpress Codex ), which will be the role assigned to verified author across the website. All Users with Admin Role are automatically Verified while user role has to be switched from either contributor/Author role to Verified Author.

    The above code was able to do 98% of the task but when a verified author / administrator comments, their display name doesn't show the verified badge. I was able to use the below code to fix that (combining the code with a shortcode).

    function my_comment_author( $author = '' ) {
        // Get the comment ID from WP_Query
    
        $comment = get_comment( $comment_ID );
    
        if ( ! empty($comment->comment_author) ) {
            if (!empty($comment->user_id)){
                $user=get_userdata($comment->user_id);
                $author=$user->display_name.' '. do_shortcode('[this_is_verified-sc]'); // This is where i used the shortcode
            } else {
                $author = $comment->comment_author;
            }
        } else {
            $author = $comment->comment_author;
        }
    
        return $author;
    }
    add_filter('get_comment_author', 'my_comment_author', 10, 1);
    

    Shortcode to return the Verified Badge if user is admin or verified author

    function add_verification_bagdge_to_authors_sc($display_name) {
        global $authordata;
    
        if (!is_object($authordata))
            return $display_name;
    
        $icon_roles = array(
            'administrator',
            'verified_author',
        );
    
        $found_role = false;
        foreach ($authordata->roles as $role) {
            if (in_array(strtolower($role), $icon_roles)) {
                $found_role = true;
                break;
            }
        }
    
        if (!$found_role)
            return $display_name;
    
        $result = sprintf('%s <i title="%s" class="fa fa-check-circle"></i>', $display_name,  __('This is a Verified Author', 'text-domain-here') );
    
        return $result;
    }
    add_shortcode( 'this_is_verified-sc', 'add_verification_bagdge_to_authors_sc' );
    

    Please Note: Not all Magazine theme are properly coded / have a hard coded Block and module elements, so the verified badge might not work on their block / module element. I experienced this during the whole website design process, so we had to change up theme and the only modification i had to make to the new theme was to remove the html escape added to their block/module code, so that the icon can be rendered. i.e. in their module/block code they had something like this esc_html( the_author() ) and i removed the esc_html to have just the_author() an it all worked.

    Its not really a straight forward solution but that is how i was able to tackle the task without using a plugin. Just add the codes to your functions.php file and that's it. I hope it helps someone.

    Thanks to Kero for pointing me in the right direction, providing the code i was able to work with and manipulate