• Resolved jshrek

    (@jshrek)


    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')); to public static function init() and then I have

    public 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

    https://www.ads-software.com/plugins/wp-slimstat/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter jshrek

    (@jshrek)

    Some further info…

    If I remove the IF statement so it sends an email for any/all pages (as opposed to only ‘/somepage’), then I get the user information sometimes on some pages, but not on others.

    Very strange behavior.

    I am using WordPress v4.6 with Slim Stat v4.4.1 with WooCommerce v2.6.4

    So I will ALWAYS get the proper logged in user information when I go to woocommerce /my-account page, but do not get any logged in user information for all other pages on the site.

    Thanks

    Plugin Author Jason Crouse

    (@coolmann)

    Hi,

    I suspect this happens because slimstat_filter_pageview_stat is called TOO EARLY in the WordPress flow for that function to be ready to return the right information. WooCommerce probably initializes the environment early, so that’s why it works in that case. See

    https://www.ads-software.com/support/topic/get_current_user_id-returns-0/#post-6106612

    Best,
    Jason

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_get_current_user() returns ID 0 when called within custom class’ is closed to new replies.