How to Change Author Link to Buddypress Profile Link

0
26

How to Change Author Link to Buddypress Profile LinkIn this blog post I will show you how to change the author link (link to the WordPress author archive page) to his BuddyPress profile page.

There are three ways to do this. I will show you all. These methods are listed according to their difficulty i.e first being most difficult.

Method 1: Replace the_author_posts_link()

You have to find all the call to function the_autho_post_link() in your theme’s files. This is usually used in index.php, single.php, page.php, archive.php files.

And replace them with:

[crayon lang=’php’]

echo bp_core_get_userlink(get_the_author_meta(‘ID’));

[/crayon]

Method 2: Add filter in function.php

Finding the the_autho_post_link() everywhere and replacing with buddpress code could be time-consuming. Thus, you can use a filter to do all the replacement. Just add the below code to your function.php file and it will create a filter and replace all links to your WordPress author profile to the BuddPress profile url.

[crayon lang=’php’]
function ra_add_author_filter() {
add_filter( ‘author_link’, ‘ra_bp_filter_author’ );
}
add_action( ‘wp_head’, ‘ra_add_author_filter’ );
function ra_bp_filter_author( $content ) {
if( defined( ‘BP_MEMBERS_SLUG’ ) ) {
if( is_multisite() ) {
$member_url = network_home_url( BP_MEMBERS_SLUG );
if( !is_subdomain_install() && is_main_site() )
$extra = ‘/blog’;
else
$extra = ”;
$blog_url = get_option( ‘siteurl’ ) . $extra . ‘/author’;
return str_replace( $blog_url, $member_url, $content );
}
return preg_replace( ‘|/author(/[^/]+/)|’, ‘/’ . BP_MEMBERS_SLUG . ‘$1’ . ‘profile/’, $content );
}
return $content;
}
[/crayon]

Method 3: Install BP-Blog-Author-Link plugin.

The code in method 2 is actually the code from bp-blog-author-link plugin by Ron Rennick. If you don’t like to edit code then simply install this plugin and it will do the trick.

Final Words:

If your theme is not very complex and you know how to play with codes then I recommend to use method 1. It will save some computing. Else go with either method 2 or 3 per your liking.

LEAVE A REPLY

Please enter your comment!
Please enter your name here