i am trying to get the User_ID outside of wordpress. I have a rating plugin and it needs the current user id, to cast votes. However it seems that something is going wrong. This is the part of the code i am using in my functions.php file:

function get_userid(){
  $user = get_current_user_id( );
  //i can correctly echo the user_id here
  return $user;
function add_thumbsup(){
   include './thumbsup/init.php';

   echo ThumbsUp::javascript();
add_action('wp_head', 'add_thumbsup');

Within init.php i have a callback function to get_userid(). So far so good.

Within the Plugin i have a block to check for the user_id:

public static function get_user_id()
    // Cache
    static $user_id = FALSE;

    // This code only needs to be executed once per request
    if ($user_id !== FALSE)
        return $user_id;

    // The callback config is left empty
    if ( ! ThumbsUp::config('user_id_callback'))
        return $user_id = NULL;

    // Load the user id
    $user_id = (int) call_user_func(ThumbsUp::config('user_id_callback'));
            //returns value from get_userid()
            //echo $user_id => 33
    // Set user id to NULL if not found
    return ($user_id) ? $user_id : $user_id = NULL;

It shows my correct user_id of 33 when echoed. However, when vote up/down is pressed, the function returns NULL.

Can it be a Ajax/Javascript related problem? Or how can i fix this?

  • What precisely do you mean by "outside"? If you are using WP plugin you should still be "inside" WP in its code.
    – Rarst
    Commented May 1, 2013 at 17:22
  • 1
    What has functions.php got to do with a plugin? And, looking at your code, you have 2 functions with the same name.
    – vancoder
    Commented May 1, 2013 at 17:35
  • oh sorry didn't mean to tag wp plugin, it is just a plugin. But not a wp plugin. I don't think i have 2 functions with the same name :/. The main Problem is: how can echo give me the correct result and when i press on a vote button, NULL is passed.
    – user32269
    Commented May 1, 2013 at 18:00
  • 2
    Then what type of plugin is it? Where are your "plugin"'s files stored in relation to the Wordpress root directory?? Are you submitting your AJAX requests to ajaxurl?
    – bosco
    Commented May 1, 2013 at 18:02

1 Answer 1


AJAX requests (which you imply you are using but do not explain/demonstrate) do not load your active theme, which is where functions.php resides (unless you were referring to a functions.php file that you created in your plugin's directory). As such, your call_user_func() is likely failing as the function that you are attempting to call is not loaded.

Note that it's considered poor form to mix plugin functionality with theme functionality.

I strongly recommend that you simply invoke get_current_user_id( ) in your plugin's files rather than this strange run-around you've currently established.

Furthermore, if your plugin needs to insert code or functionality in/near the loading of the themes you should be utilizing the Wordpress Plugin API's action hooks and filters to load and execute code where needed rather than inserting it into your theme directory.

If you wish to continue down your current path, you could try to explicitly require() your functions.php file when you first begin processing an AJAX request; know that this may have adverse side effects and I strongly recommend against it.


Your Answer

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

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