WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get current user for plugin (6 posts)

  1. willcowd
    Member
    Posted 1 year ago #

    Hi All

    I'm writing a custom plugin and need to get the data for the logged in user.

    However, none of the wp functions are working. I've tried adding in wp-blog-header.php but that created fatal errors.

    Any help would be much appreciated.
    Thanks
    Mark

  2. Chris Hardie
    Member
    Posted 1 year ago #

    When you say none of the functions are working, it would be helpful for you to provide more specific information about what functions you're trying to use, what the result is, and including any of the fatal errors you mentioned.

    There are a number of functions available to access data about the logged in user. If you haven't already, make sure you view these pages:

    http://codex.wordpress.org/Function_Reference/get_userdata
    http://codex.wordpress.org/Function_Reference/get_currentuserinfo
    http://codex.wordpress.org/Function_Reference/wp_get_current_user

  3. willcowd
    Member
    Posted 1 year ago #

    Hi Chris

    Sorry for the lack of information.
    I am able to run functions including:

    add_action('media_buttons_context', 'add_my_custom_button');
    add_filter( 'mce_css', 'filter_mce_css' );

    If I add in require_once( ABSPATH . 'wp-blog-header.php'); i get

    Fatal error: Call to a member function main() on a non-object in /home/willcowd/public_html/wp-includes/functions.php on line 798

    ...which I would expect.

    $user = get_currentuserinfo();
    echo $user;

    returns

    Fatal error: Call to undefined function get_currentuserinfo() in /home/willcowd/public_html/wp-content/mu-plugins/custom-functions.php on line 16

    I hope this explains it.
    Thanks
    Mark

  4. Chris Hardie
    Member
    Posted 1 year ago #

    It looks like this person had a similar issue:

    http://www.saphod.net/2009/01/28/call-to-undefined-function-get_currentuserinfo/

    and that the solution was requiring 'pluggable.php' before calling get_currentuserinfo. That's consistent with http://codex.wordpress.org/Function_Reference/get_currentuserinfo#Source_File which says the function is defined there.

    I am not entirely sure that this is the best or even appropriate way to accomplish what you need, but you might try loading the pluggable.php function before calling the user info function. You should also wrap your call to get_currentuserinfo with if ( !function_exists() ) to avoid fatal errors.

  5. willcowd
    Member
    Posted 1 year ago #

    That is a step forward, but I'm getting:

    WP_User Object ( [data] => [ID] => 0 [caps] => Array ( ) [cap_key] => [roles] => Array ( ) [allcaps] => Array ( ) [filter] => )

    From

    require (ABSPATH . WPINC . '/pluggable.php');
    	if ( !function_exists('wp_get_current_user') ){
    		echo 'Function not set';
    		function wp_get_current_user() {
    			global $current_user;
    			get_currentuserinfo();
    			return $current_user;
    		}
    	}
    	print_r(wp_get_current_user());
  6. willcowd
    Member
    Posted 1 year ago #

    Found the answer!!

    I was running things in the wrong order. In the end, this function worked:

    function get_user(){
    		global $current_user;
    		print_r($current_user);
    	}
    	add_action( 'admin_head', 'get_user' );

    This makes sure things are done in the correct order.

    Thanks Chris for your help

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags