WordPress.org

Ready to get started?Download WordPress

Forums

How to get Current User ID outside of WP Blog. (5 posts)

  1. MasterShake
    Member
    Posted 6 years ago #

    hi guys, I am working on an AJAX instant messenger plug in for my WP. However I want the plug in to work on my VBulletin forum as well since the user names are bridged to wordpress I am able to use the same username across WP and Vbulletin. However there is a slight problem, for this plug in to work outside of WP i include wp-config.php which loads wp-settings.php which in turns loads a ton of useless (for this plug in) things such as themes, other plug ins and filters. This causes a conflict with Vbulletin causing weird filtered output.

    All I really need is to get at $current_user from outside of WP Blog bypassing everything else that is redundant.

    is this possible?

    thanks.

  2. MasterShake
    Member
    Posted 6 years ago #

    anyone?

  3. tylermenezes
    Member
    Posted 6 years ago #

    You'll probably need to work with the cookie and database manually.

    The cookie wordpress sets is named wordpress_md5(site_url). In it you will find:

    username|expiration|validation_code

    Username is just the user's username. Expiration is the time in seconds since the UNIX epoch, you can see if it's expired in PHP with if(time() > expiration). Validation code is an hmac-hashed string. You can validate it with this function:

    function wp_validate_auth_cookie($cookie) {
    	$secret = YOUR_WORDPRESS_SECRET_KEY;
    	$salt = YOUR_WORDPRESS_SALT;
    
    	@list($username, $expiration, $hmac) = explode('|', $cookie);
    	if(!$username || !$expiration || !$hmac) return false;
    
    	if ( $expiration < time() )
    		return false;
    
    	$key = $username . $expiration . $secret . $salt;
    	$hash = hash_hmac('md5', $username . $expiration, $key);
    
    	if ( $hmac != $hash )
    		return false;
    
    	return true;
    }

    I wrote a plugin for Vanilla to work with WordPress yesterday, :D.

    The secret key and salt should be defined in wp-config.php.

  4. Iva Koleva
    Member
    Posted 6 years ago #

    You can try this:

    require_once('../../../wp-config.php');
    require_once('../../../wp-includes/wp-db.php');
    require_once('../../../wp-includes/pluggable.php');
    
    echo wp_get_current_user()->ID;

    Just make sure the path is OK, so that you are able to require the files successfully. :)

  5. mwangisk
    Member
    Posted 5 years ago #

    e-sense:
    Thanks a million.

    - After i wasted a whole day

Topic Closed

This topic has been closed to new replies.

About this Topic