0

I'm trying to learn how to interpret results from print_r for a bridge plugin I'm developing.

Background

My first issue was setting editor capabilities receiving administrator capabilities. While working on it, I realized that only the first two capabilities in the array were being added. This is now fixed. However,

Moving Forward

I've now started to go back and look at the calls being made and inserting in wp_die, var_dump, and print_r in places to see if the results are as expected.

Now comes the question. The results are not as expected and maybe someone can explain why.

The Code

A file includes the function get_userdata( $user_id ). Within this file, there is a check for is_numeric, empty, and if !$user_id return false. Next, if !$user then to call a class which a method called fillUserData.

The fillUserData is as follows:

    public static function fillUserData($visitor, $classObj, $user_id) {
    if( $user_id != 0 ) {

        global $XF;

        $userModel = $XF->getModelFromCache('XenForo_Model_User');

        $user = $userModel->getUserById($user_id, array('join' => XenForo_Model_User::FETCH_USER_FULL));

        $classObj->userObj = $user;

            echo '<pre>';
            echo $user_id;
            print_r( $classObj->userObj[username] );
            echo '</pre>';
            //die();

        if( $user_id == $visitor->get('user_id') ) {


            // doing stuff

            return $classObj;

        } else {

            // doing stuff

            return $classObj;

        }
    } 

    // return false;
    echo 'This is a mess';

}   

At this point I'm confused. I was expecting only one array returned. But several are returned within the theme (including default themes, etc).

This first one shows at the top left of the theme and is absolutely the one I expected. [Update: To clarify, this array is definitely for the currently logged in user]

    Array
(
[user_id] => 3
[username] => Albert
[email] => [email protected]
[gender] => 
[custom_title] => 
[language_id] => 1
[style_id] => 0
[timezone] => Europe/London
[visible] => 1

This next one is above the first post done by a different user. This totally is not what I was expecting. [Update: This array shows even when no one is logged into the site]

    Array
(
[user_id] => 1
[username] => LPH
[email] => [email protected]
[gender] => 
[custom_title] => 
[language_id] => 1
[style_id] => 0
[timezone] => Europe/London
[visible] => 1
[user_group_id] => 2

These arrays are being returned above another post done by a different user .... and not what I expected since there is a if ( $user_id != 0 ) conditional.

If I go into a single page then the print_r shows for that author of the post and not the user I'm logged in as ...

My question are:

Am I using $classObj->userObj = $user wrong? Why is the author of WordPress blog posts showing in a print_r when $user conditional suggests it should not?

Hopefully that is enough information for someone to explain to me why this is not working as expected. My knowledge is very weak (at best). Of course, if I should not be calling the class within a pluggable function then please suggest a better method.

Thank you!

4
  • You are asking about something that is essentially not WordPress code. I think it might be this commercial software but I'm not sure.
    – s_ha_dum
    Feb 17, 2014 at 0:51
  • This is about a WordPress plugin. Please look at the code and you'll see I'm working through a pluggable function then calling something else.Yes, I'm bridging that forum user information to WordPress.
    – LPH
    Feb 17, 2014 at 1:57
  • When I look at the code you posted (which is only part of a class), everything of significance is being done by the $XF object and that isn't WordPress code. You have not posted the code that links this code to WordPress, though you mentions some WordPress functions. And you have not explained what you are trying to accomplish. While you wrote quite a bit, you didn't explain much. Most everything is a black box. Right now, about all I have is that you probably replaced get_userdata with... something.
    – s_ha_dum
    Feb 17, 2014 at 2:24
  • I'm sorry but my question asks why the print_r is repeating on the WP site and returning different results: the first array returned is based on the expected user_id while the others are based on the roles of the accounts who made posts in the WP blog. To summarize: the get_userdata calls this class. This class fills information from the forum. Since the print_r is at this point then I didn't think there was a need to show anything else.
    – LPH
    Feb 17, 2014 at 3:50

1 Answer 1

2

get_userdata can be, and is, used to get data about different users in different contexts. You seem to expect it to return data about only one "expected" user but I'm not sure why. What you are seeing is the dumped data from several of these different contexts-- for example, for the logged in user, for the post author, etc. as get_userdata is used to retrieve information about each of these in turn.

If you look at the Codex, get_userdata can be passed a user ID. If used in that way-- say by a plugin or a theme--, it will return data about whatever user ID was passed to it. get_userdata is also used numerous time by the Core to get data about different users-- not always the currently logged in user (if that is even what you are asking).

If you expect it always to return the same user then you are using it incorrectly. It is hard to say exactly how as you have posted things so out of context. You might be looking for get_currentuserinfo(), which is also pluggable.

2
  • Thank you. I believe this is what I was trying to figure out because these arrays are being returned even when not logged into the forum. So, to just make sure I'm understanding, get_userdata replacement I have in my plugin is for everyone (even if not logged in). Basically, calling a class to fill in information about the forum in the replacement function get_userdata() is a bad place. I do have a get_currentuserinfo() that is modified in the plugin. Maybe that is the proper place to fill in forum user information.
    – LPH
    Feb 17, 2014 at 5:07
  • Thank you. Moving the call to the class fixed that issue: The print_r is now only showing for the user logged into the forum. However, now there is another problem.... the forum user isn't actually logged into WordPress any longer. Fix one thing - break another. :) I do want to thank you for the explanation - which was very helpful.
    – LPH
    Feb 17, 2014 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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