• Resolved firehold

    (@firehold)


    Hello,

    i am not able to use the current user function inside of my plugin. the user object seems not to be initilized yet where the plugin is loaded.
    Is it possible to force the user object init or is there a possibility like if not init then init?

    At my point the user id is static 0. (i wonder because some days ago it works).

    I would be very thankful for hints what i made wrong.

    code:

    defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    
    class MyPlugin {
    	
        public $userid;
    		
        protected $url;
        protected $path;
        protected $basename;
    
        public function __construct() {
            add_action( 'plugins_loaded', array( $this, 'plugin_setup' ));
        }
    
        function plugin_setup() {
            $this->url = plugin_dir_url(__FILE__);
            $this->path =  plugin_dir_path(__FILE__);
            $this->basename = plugin_basename(__FILE__);	
    
            $this->userid = get_current_user_id();
    		
        }
    }
    global $myplugin;
    $myplugin = new MyPlugin();
Viewing 9 replies - 1 through 9 (of 9 total)
  • Look here for the order of the actions and where the user is authenticated.
    https://codex.www.ads-software.com/Plugin_API/Action_Reference

    Thread Starter firehold

    (@firehold)

    Thank you very much. So when i read correctly “init” instead of “plugins_loaded” would load after user is initialize.
    Unfortunatelly this doesnt solve my issue. Am i wrong at some point?

    Moderator bcworkz

    (@bcworkz)

    I put your class, changing the action to “init”, into one of my plugins. On a theme template I added:

    global $myplugin;
    echo "Current user ID: {$myplugin->userid}";

    The result was “Current user ID: 1”

    So it all works OK for me, your issue apparently lies elsewhere.

    Thread Starter firehold

    (@firehold)

    Thank you for your answer bcworkz
    The output in a seperate template-file works for me too.

    Unfortunatelly i need the user-ID in my MyPlugin class and there the value is still 0.

    Also directly after the class in the plugin-file im not able to call the id.
    So what i have to do, to work with the userid inside my class?

    Thank you very much in advance.

    • This reply was modified 5 years, 10 months ago by firehold.
    Moderator bcworkz

    (@bcworkz)

    Apparently where you are trying to use $myplugin->userid is simply too early if using it on a template works. Clearly the ID is eventually assigned correctly. You probably need to also add whatever code needs the value to an action hook that fires after “init”.
    https://codex.www.ads-software.com/Plugin_API/Action_Reference

    You can also use filters as action hooks. As long as you return the passed value, your callback can do whatever else it likes.
    https://codex.www.ads-software.com/Plugin_API/Filter_Reference

    Thread Starter firehold

    (@firehold)

    Thank you so much for your help!

    So when i understand correctly “init” is still too early?
    Is there a hook you can recommend which already has loaded user object but before loading content? Because i need a shortcode registration which needs to fires before the content is loaded or not?

    The solution with the filters seems like a nice little hack, but isnt it wrong or not mentioned?
    It feels not good, not valid, like hardcoding…

    • This reply was modified 5 years, 10 months ago by firehold.
    Moderator bcworkz

    (@bcworkz)

    Shortcodes are processed upon content output, around the same time template code executes. If the user ID is available on a template, it should be available to the shortcode handler. If your shortcode handler is not able to obtain the user ID from your class, the ID set from the “init” action callback, then there must be something out of scope.

    I added this to my version of your OP class:

    	public function handle_shortcode( $atts ){
    		return "Current User ID: {$this->userid}";
    	}

    and to the __construct() method I added:
    add_shortcode( 'userid', [$this, 'handle_shortcode'] );

    Placing [userid] in post content does output the current user ID.

    There are usually plenty of actions to use, so there is usually no need to use filters as actions. There could be some unusual fringe needs though. It’s fine to do so, actions are in fact a type of filter internally. It’s just that the application of an action does not utilize any return value like a filter would. You could actually add your action callback to an action hook tag using add_filter() and it will still work. They are that closely related.

    It’s only “not good” in the sense that using a filter as an action could confuse others down the line, contrary to the principle of writing clear, easy to follow, well documented code. But if you inline document that you are // using filter as action hook because no action is available, I don’t see any problem.

    Thread Starter firehold

    (@firehold)

    Thank you so much for your detailed answer and explanation.

    I dont know whats happened but after some trys i set the action hook back to init and now all works fine. I can use the user id inside of my class without a special template site. Also the shortcode works.

    Im very happy. Thank you so much for all your help!

    Moderator bcworkz

    (@bcworkz)

    Your’e welcome! When that happens to me I blame it on caching. I’ve no reason to think that, but it’s plausible often enough ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Plugin – current user not set – user id 0’ is closed to new replies.