Based on answers like this one, I made a small plugin to display a theme displaying a “Coming soon” theme, whilst our team of editors can fill in the final theme.

It worked yesterday, but today, even though I’m logged in, I only see the “waiting” theme.
I logged out and logged back in, but still, I’m seeing the “waiting” theme, as if the is_user_logged_in() function returned false. Are these hooks too early to check for user authentification?

<?php

add_filter('template', 'pxln_change_theme');
add_filter('stylesheet', 'pxln_change_theme');

function pxln_change_theme($theme) {    
    if ( ! is_user_logged_in() ) {
        $theme="waiting";
    }
    return $theme;
}

2 Answers
2

Filters, unlike actions, don’t run at a specific moment, but when the function they are attached too, is called. The template-filter is called from get_template and the stylesheet-filter from get_stylesheet.

Typically, these functions are called to enqueue styles and scripts using wp_enqueue-scripts, an action that takes place after the current user has been set. However, it is not uncommon to see these functions also called from a function that is attached to after_setup_theme, which is fired before the current user has been set.

So, you will have to check your theme for the use of get_template and get_stylesheet (or a function that uses them, which you can find under ‘used by’ in the links above). Then check if the function is attached to a hook that is too early.

Leave a Reply

Your email address will not be published. Required fields are marked *