我正在使用Buddypress和一个名为Teambooking的插件。Teambooking插件不知道Buddypress的存在,它使用默认的Wordpress作者查询字符串(/?author=“id”,其中id是用户id号)创建并显示指向作者页面的链接。但是Buddypress为用户提供了个人资料页面。因此,我希望能够捕捉到有人单击标准作者页面链接(/?author=“id”)并将页面重新定向到Buddypress用户配置文件页面。我想我可以使用钩子“模板重定向”来捕获这个事件,但这不起作用,因为钩子只在加载目标页面时被触发,而现在发生的是Wordpress正在自动重定向包含/?author=“id”将查询字符串添加到索引。php页面,因为没有作者。php页面。
我也不能使用$_SERVER[“QUERY\u STRING”]来解析URL,因为正如我所说,Wordpress会自动重定向到索引。php,删除查询字符串。
我也在想我可以创建一个author.php页面,这样WordPress就可以停止重定向到index.php,然后使用template_redirect钩子将author.php重定向到BuddyPress个人资料页面,但这并不起作用要么。我在主题目录上创建了author.php,但WordPress继续重定向到index.php.
关于如何从/重定向的任何想法?作者="id"查询到正确的BuddyPress个人资料页面的用户?
对于不知道安装了Buddypress的插件,这种情况必须一直发生。
谢啦
-马莱娜
原来免费版本的Yoast SEO插件将作者页面的查询(?author=id)重定向到索引。php自动运行,我没有意识到。因此,我必须停用Yoast SEO插件,然后使用is_author()来检测模板重定向钩子中何时请求了作者存档页(?author=id query string in URL),以便将调用重定向到相应的BuddyPress用户配置文件。下面是我在函数中使用的代码。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' );
Buddypress不使用默认的permalink结构,因此您可能找的不是作者id。例如,如果您的permalink结构被设置为Post name,那么这将起作用。
/* 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' );