Monday, February 27, 2012

Code for getting Wordpress logged user from external php file



We used a Session variable for getting the Wordpress logged in user id from an external php file.
But it didn't work once after upgrading the wordpress. Obviously the session variable setting page was overwritten by new wordpress update.

So, I have decided to use the wordpress object to get the userid instead of session variable so that any future wordpress update won't affect our code.

I used below piece of code in the external php file, and it was working fine.

require_once('../wp-config.php');
require_once('../wp-includes/wp-db.php');
require_once('../wp-includes/pluggable.php');
global $current_user;
get_currentuserinfo();

$userid= $current_user->ID;

You can subscribe to our Email posts, and you can bookmark this blog for further reading, or you can subscribe to our blog feed.

No comments:



Thanks for visiting our blog


twitterrss facebook linkedin YouTube subscribe


Subscribe via email

Enter your email address: