1

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?

4

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

3
  • Toscho, "comment_author" from what I understand return the full name. Is that incorrect?
    – m-torin
    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.
    – fuxia
    Nov 9 '10 at 22:07
0

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

1
  • Yes they would need to be registered. In this application users must be logged in to access the form.
    – m-torin
    Nov 9 '10 at 0:57

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.