我使用的是Buddypress和一个叫做Teambooking的插件。Teambooking插件并不知道Buddypress的存在,它使用默认的Wordpress作者查询字符串(/?author=" id ",其中id是用户id号)来创建和显示指向作者页面的链接。但是Buddypress为用户提供了个人资料页面。因此,我希望能够在有人点击标准作者页面链接(/?author="id")时捕捉到,并将页面重定向到Buddypress用户个人资料页面。我认为我可以使用'template-redirect‘钩子来捕获这个事件,但这并不起作用,因为钩子只在目标页面加载时触发,并且发生的情况是Wordpress会自动将具有/?author="id“查询字符串的URL重定向到index.php页面,因为没有author.php页面。
我也不能使用$_SERVER"QUERY_STRING“来解析URL,因为正如我所说的,Wordpress会自动重定向到index.php,删除查询字符串。
我还在想,我可以创建一个author.php页面,让WordPress停止重定向到index.php,然后使用'template_redirect‘钩子将author.php重定向到buddypress的个人资料页面,但这也不起作用。我在主题的目录下创建了author.php,但是Wordpress继续重定向到index.php。
关于如何从/?author="id“查询重定向到用户正确的buddypress配置文件页面,您有什么想法吗?
对于那些不知道安装了Buddypress的插件,这种情况肯定会一直发生。
谢谢
-Malena
发布于 2017-07-26 04:47:13
原来Yoast SEO插件的免费版本会自动将作者页面(?author=id)的查询重定向到index.php,而我并不知道这一点。因此,我不得不停用Yoast SEO插件,然后使用is_author()来检测在模板重定向钩子中何时请求作者存档页面( URL中的?author=id查询字符串),以便将调用重定向到适当的BuddyPress用户配置文件。下面是我在functions.php中使用的代码,它执行重定向:
/* Redirect author page to BuddyPress page */
function my_page_template_redirect()
{
/** Detect if author archive is being requested and redirect to bp user profile page */
if( is_author() )
{
global $wp_query;
if(isset($wp_query->query_vars['author'])) {
$userID = urldecode($wp_query->query_vars['author']);
}
$url = bp_core_get_user_domain($userID);
wp_redirect( $url );
exit();
}
add_action( 'template_redirect', 'my_page_template_redirect' );发布于 2017-07-25 10:41:48
Buddypress不能使用默认的固定链接结构,所以它可能不是你要找的作者id。例如,如果您的固定链接结构设置为帖子名称,那么这将会起作用。
/* Redirect author page to buddypress page */
function my_page_template_redirect()
{
if( is_author() )
{
global $wp_query;
$user = get_user_by( 'slug', $wp_query->query['author_name'] );
$url = bp_core_get_user_domain($user->ID);
wp_redirect( $url );
exit();
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );https://stackoverflow.com/questions/45292825
复制相似问题