Display WordPress commenter nice name

I’m working on a custom plugin that needs to display specifically the commenter’s nice name. The codex shows how to do this for the logged in user but not someone else. Can this easily be done?

Related posts

Leave a Reply

2 comments

  1. wp_get_current_commenter() returns an array, the entry 'comment_author' stores the name:

    Array (
        ['comment_author']       => 'Harriet Smith,
        ['comment_author_email'] => 'hsmith@,example.com',
        ['comment_author_url']   => 'http://example.com/'
    )
    

    More information is available in the codex.

    Update

    To find the nice name, ask the DB:

    /**
     * Searches the user table by display name.
     * @param string $display_name
     * @return object
     */
    function get_user_by_display_name( $display_name )
    {
        global $wpdb;
        $user = $wpdb->get_row( 
            $wpdb->prepare("SELECT * FROM $wpdb->users WHERE display_name = %s", $display_name) 
        );
    
        if ( ! $user )
        {
            return FALSE;
        }
    
        _fill_user($user);
    
        return $user;
    }
    
    // Usage:
    if ( $userdata = get_user_by_display_name( 'Thomas Scholz' ) )
    {
        print $userdata->user_nicename;
    }
    

    Caveat: Not tested. 🙂