Php 根据用户是否登录更改WordPress中的首页
我正在构建一个插件,帮助用户从WordPress前端注册和登录,并根据用户是否登录隐藏和显示资源 我遇到的一个问题是如何根据用户的登录/注销状态更改根域中显示的主页。在主题模板中,使用以下结构可以轻松实现这一点:Php 根据用户是否登录更改WordPress中的首页,php,wordpress,Php,Wordpress,我正在构建一个插件,帮助用户从WordPress前端注册和登录,并根据用户是否登录隐藏和显示资源 我遇到的一个问题是如何根据用户的登录/注销状态更改根域中显示的主页。在主题模板中,使用以下结构可以轻松实现这一点: if ( is_user_logged_in() ) { // Logged-in user content } else { // Logged-out user content } 因为这是一个插件,但是,我不希望网站管理员必须混乱与他们的主题文件。到目前为止,我
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\u选项
函数独立工作,但封装在if语句中会抛出致命错误,因此站点根本不会加载
我还尝试使用add\u rewrite\u规则
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
它做两件事:
- 更改登录用户的“主页”模板,在活动主题目录中查找该模板,回退到插件包含的版本
- 用您想要的任何数据替换内容(本例中为“Hello World”帖子)
set\u home\u 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 );
}
这应该会给你一些想法,但正如你所指出的,可能没有真正的“自动”解决方案。用户仍需调整主题文件/添加页面或其他内容。但这可能是一个伟大的插件文档的一部分;) 当你包装在if语句中时,返回的致命错误是什么?在最新的WP版本中,我没有收到致命错误,但我也不确定这是否符合你的想法。update_option()按字面意思更新WP Options表。从本质上讲,你的代码试图做的事情最终会不断地改变页面首页的值,并在每次有人登录时在首页上显示。Chris-仔细想想,如果我登录/注销,主页在哪些方面会有所不同?这是一个完全不同的页面吗?内容是静态的吗?@ChrisRasco-对于会员网站或web应用程序,我可以想象一个登录页面,为注销用户提供行动呼吁,为内部用户提供内容提要或其他内容。有点像Facebook或Twitter的工作方式——无论你是否登录,它都是相同的URL,但内容不同。@JesseKernaghan-我不确定。Chrome会返回一条“页面加载失败”的消息,所以它甚至不会打印错误消息。这是一个有趣的方法。这将考虑布局,但不一定要考虑内容(取决于模板的结构)。我看得越多,我就越需要依赖用户从主题的角度来理解这一部分。谢谢你!不客气。更新了我的答案并添加了“仅插件”方法。这会给你更多的想法。祝你好运谢谢你,彼得。这并不完全是我想要做的,但根据我所有的研究,这是最接近我所期望的方法。对于生产,我选择由用户决定。如果你想看成品,可以在这里找到:谢谢你花时间帮我。非常感谢!