0

I've set all author links to head to the author's buddypress profile rather than the author archive page. I've done this through my child theme's function.php file.

 *add_filter( 'author_link', 'change_author_link', 10, 1  );

  function change_author_link($link) {
   $username=get_the_author_meta('user_nicename');


  $link = 'http://localhost/MYSITE.com/members/' . $username;
   return $link;
 } 

BUT

The issue I now have is that I have added a tab to the buddypress profile to link to the author archive so you can see their posts, but with the above code active, that link will no longer work.

Here's the code for the buddypress tab, added through a bp-custom.php file:

//set up new nav item in buddypress profile
add_action( 'bp_setup_nav', 'add_author_link_to_bp_profile' );

function add_author_link_to_bp_profile() {

    $post_count = count_user_posts_by_type(bp_displayed_user_id());

    if( $post_count > 0 ) {
        bp_core_new_nav_item( array(
        'name'                    => 'Posts ('.$post_count.')',
        'slug'                    => 'author',
        'position'                => 100,
        'show_for_displayed_user' => true,
        'screen_function'         => 'redirect_user_to_author_page',
    ));
    }

}

//redirect link to author archive page
function redirect_user_to_author_page(){
    global $bp;
    wp_redirect( get_author_posts_url( $bp->displayed_user->id ) );
    exit;
}

function count_user_posts_by_type( $userid, $post_type = 'post' ) {
    global $wpdb;

    $where = get_posts_by_author_sql( $post_type, true, $userid );

    $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );

    return apply_filters( 'get_usernumposts', $count, $userid );
}

I have 2 options: 1) Figure out a way to make the buddypress-archive link work just on the member profile page 2)Manually change the author links across the site in the templates (rather than in the function file)

NOTE: I have manually changed most of the links across the site, and that would work, but I'm having issues changing the links of the homepage - which is built with WPBakey - the visual builder.

If anyone has thoughts on how to change the author links ONLY on the homepage - even through the function.php file - so it won't affect the Buddypress author-archive link/tab, that would also work.

The issue with the current function.php code is that it redirects ALL author-archive links, making the new buddypress tab ineffective. If I can figure out how to add something that will only redirect the links if they're part of the homepage (considering I've been able to change the rest of the links on the site through their templates) that would be great.

0

Rather than add your filter immediately when the functions.php file loads, find actions that you can hook onto later, when you know the context of the current request, so you can add and remove filters in a targeted way.

For example, you might be able to use loop_start to only affect WPBakery output in the main content area:

add_action( 'loop_start', 'wpd_loop_start_example' );
function wpd_loop_start_example( $query ){
     if( $query->is_main_query() && is_home() ){
        // The loop for the main query is starting, and it's the home page
        add_filter( 'author_link', 'change_author_link', 10, 1 );
     }
}

You can also remove filters, so later content won't be altered:

add_action( 'loop_end', 'wpd_loop_end_example' );
function wpd_loop_end_example( $query ){
     if( $query->is_main_query() && is_home() ){
        remove_filter( 'author_link', 'change_author_link', 10 );
     }
}

template_redirect or wp actions might be other candidates for adding filters after the main query is set up but before output starts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.