我不确定我是否理解您的设置,但这里有一些想法:
A) 显示登录链接redirect_to
参数集:
您可以将以下内容添加到自定义模板页面:
if( ! is_user_logged_in() )
{
printf( \'<a href="%s">%s</a>\',
wp_login_url( get_permalink() ),
__( \'You need to login to view this page!\' )
);
}
这将为匿名访问者生成一个登录链接,当前页面位于
redirect_to
获取参数。
B) 重定向到wp-login.php
使用redirect_to
参数集:
请注意
wp_redirect()
必须在发送HTTP标头之前发生。
我们可以在template_redirect
挂钩:
add_action( \'template_redirect\',
function()
{
if( ! is_user_logged_in()
&& is_page( array( \'member-page-1\', \'member-page-2\' ) )
)
{
wp_safe_redirect( wp_login_url( get_permalink() ) );
exit();
}
}
);
我们限制访问带有slug的页面
member-page-1
和
member-page-2
.
C) 本机登录表单(直列):
另一种选择是将本机登录表单直接包含到页面内容中:
add_filter( \'the_content\', function( $content ) {
if( ! is_user_logged_in()
&& is_page( array( \'member-page-1\', \'member-page-2\' ) )
)
$content = wp_login_form( array( \'echo\' => 0 ) );
return $content;
}, PHP_INT_MAX );
我们限制访问带有slug的页面
member-page-1
和
member-page-2
.
请注意,您必须处理归档/索引/搜索页面
更新:我使用wp_login_url()
作用