繁体   English   中英

根据用户是否登录来更改WordPress的首页

[英]Changing the front page in WordPress based on whether the user is logged in or not

我正在构建一个插件,该插件可以帮助用户从WordPress前端进行注册和登录,还可以根据用户是否登录来隐藏和显示资源。

我要坚持的一个方面是如何根据用户的登录/注销状态更改在根域显示的主页。 在主题模板中,可以使用以下结构轻松实现此目的:

if ( is_user_logged_in() ) {
    // Logged-in user content
} else {
    // Logged-out user content
}

但是,因为这是一个插件,所以我不希望站点管理员必须弄乱他们的主题文件。 到目前为止,我已经尝试添加此内容以动态重写首页:

if ( is_user_logged_in() ) {
  $about = get_page_by_title( 'Front Page Logged In' );
  update_option( 'page_on_front', $about->ID );
  update_option( 'show_on_front', 'page' );
} else {
  $about = get_page_by_title( 'Front Page Logged Out' );
  update_option( 'page_on_front', $about->ID );
  update_option( 'show_on_front', 'page' );
}

update_option函数可以update_option工作,但是包裹在if语句中,会引发致命错误,因此根本无法加载该站点。

我也尝试过使用add_rewrite_rule API来简单地告诉WordPress将根域视为另一个页面。 这在指定特定页面时有效,但是我不知道如何使它适用于根(且仅根)URL,即使可以,当包裹在if语句中时,它也无法正常工作。

function add_my_rule() {
    if ( is_user_logged_in ) {
        add_rewrite_rule('test','index.php?pagename=loggedin','top');
    } else {
      add_rewrite_rule('test','index.php?pagename=loggedout','top');
    }
}
add_action('init', 'add_my_rule');

因此,回顾一下,我需要一种方法,如果用户登录,则将其显示为首页,而如果用户注销,则将其显示为来自插件(而不是主题文件)的另一页。 对于如何使这项工作的任何见解将不胜感激!

方法一

这应该根据模板文件来完成。 您仍然需要让用户添加/构建模板,或者在激活插件后将其复制到主题目录。

add_filter( 'template_include', 'member_home' );
function member_home( $template ) {
    if ( is_home() || is_front_page() ){
        if ( is_user_logged_in() ) {
            return locate_template('home-member.php');
        } else {
            return get_home_template();
        }
    }
    return $template;
}

方法二

这是另一种方法,仅使用插件。 假设您的插件文件夹中有一个templates/目录,其中包含文件member-home.php

它有两件事:

  • 更改登录用户的“主页”模板,在活动主题目录中查找该模板,回退到包含插件的版本
  • the_content的任何数据替换the_content (在此示例中拉出“ Hello World”帖子)

您也可以通过register_activation_hook()添加一个新页面,并在set_home_content()函数中查询该数据。

define('MYPLUGIN_PLUGIN_PATH', plugin_dir_path( __FILE__ ));
define('ACTIVE_THEME_PATH', get_stylesheet_directory());

add_action('plugins_loaded', 'myplugin_plugins_loaded');

function set_home_template( $template ){
    if ( is_home() || is_front_page() ){
        if ( is_user_logged_in() ){
            if( file_exists(ACTIVE_THEME_PATH.'templates/member-home.php') ) {
                return ACTIVE_THEME_PATH.'templates/member-home.php';
            } else {
                return MYPLUGIN_PLUGIN_PATH.'templates/member-home.php';
            }
        } else {
            return get_home_template();
        }
    }
    // Returns the template
    return $template;
}

function set_home_content( $content ){
    if ( is_home() || is_front_page() ){
        if ( is_user_logged_in() ){
            $args = array(
                'pagename' => 'hello-world',
                'posts_per_page' => 1
            );
            $posts = new WP_Query( $args );
            while ( $posts->have_posts() ) {
                $posts->the_post();
                $content = get_the_content();
            }
            wp_reset_postdata();
            // Returns the custom queried content
            return $content;
        }
    }
    // Returns the default content.
    return $content;
}

function myplugin_plugins_loaded() {
     add_filter( 'template_include', 'set_home_template' );
     add_filter( 'the_content', 'set_home_content', 10 );
}

那应该给您一些想法,但是正如您指出的那样,可能没有真正的“自动”解决方案。 用户仍然必须修改主题文件/添加页面或其他内容。 但这可能是很棒的插件文档的一部分;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM