jeudi 10 mars 2016

Wordpres php can't get current user ID in a plugin

I have been struggling to get the current user ID in a wordpres plugin I am building. I have tried what seems to be everything, with still no luck.

The closest I have got is being able to return a '0' and then the user ID (so in the case of admin, I can return "ID: 0 ID: 1".

Here is the code that does that for me:

add_action('admin_init', 'myFunction');

function myFunction(){
    $user_ID= get_current_user_id();   
    echo "ID: ".$user_ID;
}

Obviously I have wrapped it in a function here that fires on admin init.

What's particularly strange is that in other files in my plugin, I am able to get user info using

global $current_user;
get_currentuserinfo();
$current_user->ID

But this trigger a fatal error

Fatal error: Call to undefined function get_currentuserinfo() 

...even if I wrap it in my function.

Surely this is a simple thing to achieve? Can anyone tell me how I do this



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire