针对未登录用户的特定主题

时间:2014-04-25 作者:TheMadcore

我想为未登录用户创建一个特定主题,但我不知道如何创建函数或插件来为他们转换主题,并为登录用户留下另一个特定主题。

有人知道怎么做吗?我唯一的线索就是函数switch_themeis_user_logged_in, 但我不知道如何让他们做到这一点。

谢谢你的时间。

2 个回复
SO网友:kaiser

功能-发动机罩下is_user_logged_in() 可用于确定来宾和已登录(因此已注册)用户之间的差异,switch_theme( $stylesheet ) 更改中的实际数据库条目{$wpdb->options} 表格:

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() );
简单的解决方案:两个样式表

$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
);
如您所见,我添加了commons.css 到样式表。这将是另一个以前注册/排队的样式表,其中包含双方共享的所有定义。

SO网友:TheDeadMedic

您可以使用覆盖WordPress使用的主题templatestylesheet 过滤器:

/**
 * Override the current theme to show non-logged in users.
 * 
 * @link    http://wordpress.stackexchange.com/q/142418/1685
 * 
 * @param   string  $theme
 * @return  string
 */
function wpse_142418_nopriv_theme( $theme ) {
    if ( ! is_user_logged_in() ) 
        $theme = \'mythemefoldername\';
    return $theme;
}

add_filter( \'stylesheet\', \'wpse_142418_nopriv_theme\' );
add_filter( \'template\',   \'wpse_142418_nopriv_theme\' );

结束

相关推荐

在条件WITH GET_USERS中使用USER_STATUS返回现有用户

我正在开发一个插件,需要以特定的方式返回化身。如果wp\\u users表中存在该用户,则运行该代码;如果该用户不在wp\\u users表中,则运行另一组代码。我无法使用username\\u exist,现在正在尝试使用user\\u status值0。从Codex中,代码可以根据user\\u状态返回信息$blogusers = get_users(\'user_status=0\'); foreach ($blogusers as $user) { echo \'<l