malena
malena

Reputation: 948

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

I am using Buddypress and a plugin called 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. But Buddypress provides profile pages for users. 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. 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.

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.

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. I created author.php on my theme's directory but Wordpress continues to redirect to index.php.

Any ideas on how to this redirection from the /?author="id" query to the proper buddypress profile page of the user?

This must happen all the time with plugins that are not aware of Buddypress being installed .

thanks

-Malena

Upvotes: 0

Views: 2052

Answers (2)

malena
malena

Reputation: 948

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. 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. Here is the code I used in functions.php, which does the redirection:

/* 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' );

Upvotes: 1

rob-gordon
rob-gordon

Reputation: 1488

Buddypress doesn't work with the default permalink structure, so it probably isn't the author id that you're looking for. If your permalink structure were set to Post name for instance, then this would work.

/* 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' );

Upvotes: 0

Related Questions