WordPress.org

Ready to get started?Download WordPress

Forums

Retrieve User ID in 2.0 RC1 (4 posts)

  1. Ryyo
    Member
    Posted 8 years ago #

    Howdy all,

    I've got a blog in which I need to allow certain functionality for users depending on their role in WP 2.0. I know that the permissions system has moved the user level to the wp_usermeta table, so I'm wondering if there is still an easy way for me to retrieve the current users ID number in my PHP, therefore allowing me to look up their user level.

    Alternatively, I don't suppose there's a quick and easy way to simply return subscriber, contributor, editor, etc is there? That'd be perfect...

    ;-)

    [edit]

    I found this get_usermeta(); function in the functions.php file:

    function get_usermeta( $user_id, $meta_key = '') {
    global $wpdb;
    $user_id = (int) $user_id;

    if ( !empty($meta_key) ) {
    $meta_key = preg_replace('|a-z0-9_|i', '', $meta_key);
    $metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key'");
    } else {
    $metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id'");
    }

    if ( empty($metas) ) {
    if ( empty($meta_key) )
    return array();
    else
    return '';
    }

    foreach ($metas as $index => $meta) {
    @ $value = unserialize($meta->meta_value);
    if ( $value === FALSE )
    $value = $meta->meta_value;

    $values[] = $value;
    }

    if ( count($values) == 1 )
    return $values[0];
    else
    return $values;
    }

    This looks like it's capable of returning the user level that I need, if I can feed it the current user's ID. I'm not sure how to get the values out that it returns though: I hard-coded my user ID and meta key in, but obviously it doesn't return anything if I don't make a reference to it's returned variables. Any ideas?

  2. MichaelH
    Member
    Posted 8 years ago #

    Can't be much help but to point out this thread:
    http://wordpress.org/support/topic/50007#post-275673

  3. Ryan Boren
    WordPress Dev
    Posted 8 years ago #

    if ( current_user_can('edit_files') )

    Replace edit_files with the capability you are interested in querying. A list of capabilities can be found in upgrade-schema.php. If you want to see if a user has the equivalent to the old level 9 privs:

    if ( current_user_can('level_9') )

    Each role has capabilities that map to the old user level system.

  4. Ryyo
    Member
    Posted 8 years ago #

    Whoa, interesting! This may be just what I need, I'll try it out right now.

    [edit]

    Terrific, that seems to have done it! Thanks a ton, I'm trying to get my site up by the end of the week and I was starting to fear that this was going to be a major hurdle.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.