WordPress.org

Ready to get started?Download WordPress

Forums

get_userdata not pluggable (1 post)

  1. s_ha_dum
    Member
    Posted 5 years ago #

    Hello.

    I'm hoping someone can explain this behavior to me.

    I've added some additional fields to my user profile page (profile.php and edit-user.php). This additional data is saved to table I created for the purpose. So far no problem.

    To populate these fields I wrote a replacement for get_userdata() basically adding a join to the table that contains the extra data. I've included the code below but the only thing different from the pluggable.php version is the query string itself. This works for an administrator editing other people's profiles but it does not work for anyone editing his or her own profile. That is, if I log in as 'admin' and edit some other user's profile things work like I expect. I get my extra data. If I try to edit admin's profile or log in as some other user and try to edit that profile it doesn't work. I don't get any of my additional data but only the WP default stuff.

    What really has me puzzled is that if I don't use wp_cache it seems to work in all cases (but of course without any caching).

    if (!function_exists('get_userdata')) {
      function get_userdata( $user_id ) { // replacement for get_userdata in wp-includes/pluggable.php
        global $wpdb;
        $user_id = absint($user_id);
        if ( $user_id == 0 ) return false;
        $user = wp_cache_get($user_id, 'users');
        if ( $user ) return $user;
        if ( !$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users LEFT JOIN myuserdata ON $wpdb->users.ID = yscarddata.user_ID WHERE ID = %d LIMIT 1",$user_id))) return false;
        _fill_user($user);
        return $user;
      }
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.