2

我正在构建一个插件,帮助用户从 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函数独立工作,但包装在 if 语句中,会引发致命错误,因此网站根本不会加载。

我还尝试使用add_rewrite_ruleAPI 来简单地告诉 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');

因此,回顾一下,我需要一种方法,如果用户登录,则显示一个页面作为首页,如果用户退出,则从插件(而不是主题文件)显示不同的页面。任何有关如何完成这项工作的见解将不胜感激!

4

1 回答 1

3

方法一

这应该基于模板文件来解决问题。您仍然需要让用户添加/构建模板或在插件激活时将它们复制到主题目录。

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为您想要的任何数据(在此示例中提取了“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 );
}

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

于 2013-09-28T06:08:14.533 回答