I want to make an specific theme for non logged in users, but I don´t know how to make the function or plugin to shift the theme for them, and left another specific theme for logged in users.
Anyone knows how to do this? The only clues I had are the functions switch_theme
and is_user_logged_in
, but don´t know how to make them work to do this.
Thanks for your time.
2 Answers
Functions – under the hood
While is_user_logged_in()
can be used to determine the difference between a guest and a logged in (and therefore registered) user, switch_theme( $stylesheet )
alters actual database entries in the {$wpdb->options}
table:
update_option( 'template', $template );
update_option( 'stylesheet', $stylesheet );
update_option( 'current_theme', $new_name );
if ( count( $wp_theme_directories ) > 1 ) {
update_option( 'template_root', get_raw_theme_root( $template, true ) );
update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) );
} else {
delete_option( 'template_root' );
delete_option( 'stylesheet_root' );
}
update_option( 'theme_switched', $old_theme->get_stylesheet() );
Simple solution: Two stylesheets.
Therefore I wouldn’t recommend doing so. Simply switch “themes” – read: loaded stylesheets – on a user/guest basis
$stylesheet = plugins_dir_url( __FILE__ ).'assets/';
$stylesheet .= is_user_logged_in()
? 'style-user.css'
: 'style-guest.css;
wp_enqueue_style(
'main-stylesheet',
$stylesheet
array( 'commons.css' )
1.0
);
As you can see, I add a dependency of commons.css
to the stylesheet. This would be another, previously registered/enqueued stylesheet that has all definitions that are shared between both.