wp_get_current_user() returns ID 0 when called within custom class
-
Hi
Playing with custom reports code from here https://slimstat.freshdesk.com/support/solutions/articles/5000528531-how-do-i-add-my-own-custom-reports-to-slimstat-
I have added
add_filter('slimstat_filter_pageview_stat', array( __CLASS__, 'my_function'));
topublic static function init()
and then I havepublic static function my_function( $_stat = array() ) { if ( $_stat[ 'resource' ] == '/somepage' ) { $current_user = wp_get_current_user(); mail("[email protected]", "Slim Stat TEST", print_r($current_user, true)." ------ ".print_r($_stat, true) ); } return $_stat; }
This will succesfully send me an email whenever somebody visits /somepage
When not logged in, wp_get_current_user will correctly return ID=0 but then when a user logs in, it still returns the same ID=0 as if no user were logged in.
Although the function itself is working, why is it not returning the logged in WP user info? Is this because the code is wrapped in custom class?
Thanks
- The topic ‘wp_get_current_user() returns ID 0 when called within custom class’ is closed to new replies.