4

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;
}
1
  • Updated my answer. Tested it locally, and it works. Happy coding.
    – yivi
    Dec 29 '16 at 16:21
1

Try hooking on all these:

(But I guess you'd have to do it from a plugin, since doing it from theme functions.php might be too late).

E.g.:

/*
Plugin Name: test
Description: switchtest
Version: 0.1.0
*/

add_filter( 'template', 'yourthing_switch_theme' );
add_filter( 'option_template', 'yourthing_switch_theme' );
add_filter( 'option_stylesheet', 'yourthing_switch_theme' );
add_filter( 'pre_option_stylesheet', 'yourthing_switch_theme' );

function yourthing_switch_theme( $theme )
{
   if ( is_user_logged_in() ) {
        return $theme;
   }
   else {
      return 'waiting';
   }
}
2
  • Thank you for contributing. Unfortunately, that does not solve the issue.
    – pixeline
    Dec 28 '16 at 16:31
  • just tried the updated version (finally managed to find time to install wp locally), and works as advertised. :)
    – yivi
    Dec 29 '16 at 16:21
1

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.

2
  • 1
    can't agree with the first words of the answer. Actions are filters thta do not return value, that is whole of the difference between them. Dec 28 '16 at 12:42
  • Thanks, that looks like a probable cause. My "main" theme uses get_template_part and get_stylesheet_uriextensively, so that must be it. I just wished there 'd be a cross-theme, reliable solution. :(
    – pixeline
    Dec 28 '16 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.