简体   繁体   English

如何将作者 URL 查询重定向到 Wordpress 中的 buddypress 用户个人资料页面

[英]How to redirect author URL query to buddypress user profile page in Wordpress

I am using Buddypress and a plugin called Teambooking.我正在使用 Buddypress 和一个名为 Teambooking 的插件。 The Teambooking plugin doesn't know of the existence of Buddypress and it uses the default Wordpress author query string (/?author="id", where id is the user id number) to create and display a link to an author's page. Teambooking 插件不知道 Buddypress 的存在,它使用默认的 Wordpress 作者查询字符串(/?author="id",其中 id 是用户 ID 号)创建并显示指向作者页面的链接。 But Buddypress provides profile pages for users.但是 Buddypress 为用户提供个人资料页面。 So I want to be able to catch when someone clicks on the standard author page link (/?author="id") and re-direct the page to Buddypress user profile page.所以我希望能够捕捉到有人点击标准作者页面链接 (/?author="id") 并将页面重定向到 Buddypress 用户个人资料页面。 I thought I could use the hook 'template-redirect' to catch this event, but that doesn't work, because the hook only get's triggered when the destination page is being loaded, and what is happening is that Wordpress is automatically redirecting the URL that has the /?author="id" query string to the index.php page, because there is no author.php page.我以为我可以使用钩子“模板重定向”来捕获此事件,但这不起作用,因为钩子仅在加载目标页面时触发,而发生的事情是 Wordpress 自动重定向 URL有 /?author="id" 查询字符串到 index.php 页面,因为没有 author.php 页面。

I cannot use $_SERVER["QUERY_STRING"] either to parse the URL , because as I said, Wordpress automatically redirects to index.php, dropping the query string.我不能使用 $_SERVER["QUERY_STRING"] 来解析 URL,因为正如我所说,Wordpress 自动重定向到 index.php,删除查询字符串。

I also was thinking that I could create an author.php page, so that WordPress stops redirecting to index.php, and then use 'template_redirect' hook to redirect the author.php to the buddypress profile page, but that didn't work either.我还想我可以创建一个 author.php 页面,以便 WordPress 停止重定向到 index.php,然后使用“template_redirect”钩子将 author.php 重定向到 buddypress 个人资料页面,但这也不起作用. I created author.php on my theme's directory but Wordpress continues to redirect to index.php.我在我的主题目录中创建了 author.php,但 Wordpress 继续重定向到 index.php。

Any ideas on how to this redirection from the /?author="id" query to the proper buddypress profile page of the user?关于如何从 /?author="id" 查询重定向到用户的正确 buddypress 个人资料页面的任何想法?

This must happen all the time with plugins that are not aware of Buddypress being installed .对于不知道正在安装 Buddypress 的插件,这种情况必须一直发生。

thanks谢谢

-Malena -玛莲娜

It turns out that the free version of Yoast SEO plugin was redirecting a query for the author page (?author=id) to index.php automatically, and I wasn't aware of it.事实证明,免费版本的 Yoast SEO 插件会自动将作者页面 (?author=id) 的查询重定向到 index.php,而我并不知道。 So I had to deactivate Yoast SEO plugin, and then used is_author() to detect when the author archive page was being requested (?author=id query string in URL ) inside a template-redirect hook in order to redirect the call to the appropriate BuddyPress user profile.所以我不得不停用 Yoast SEO 插件,然后使用 is_author() 检测何时在模板重定向钩子中请求作者存档页面(?author=id 查询字符串在 URL 中),以便将调用重定向到适当的BuddyPress 用户个人资料。 Here is the code I used in functions.php, which does the redirection:这是我在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' );

Buddypress doesn't work with the default permalink structure, so it probably isn't the author id that you're looking for. Buddypress 不适用于默认的永久链接结构,因此它可能不是您要查找的作者 ID。 If your permalink structure were set to Post name for instance, then this would work.例如,如果您的永久链接结构设置为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' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM