Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

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. :)

share|improve this answer
    
Toscho, "comment_author" from what I understand return the full name. Is that incorrect? –  mattrepublic Nov 9 '10 at 0:58
    
I added some example text so it's better to understand. –  hakre Nov 9 '10 at 18:36
    
Added a function to find the nice name for a given display name. –  toscho Nov 9 '10 at 22:07
add comment

Don't they need to be registered to even get a nice name? Otherwise it'd be whatever they provide when making the comment.

share|improve this answer
    
Yes they would need to be registered. In this application users must be logged in to access the form. –  mattrepublic Nov 9 '10 at 0:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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