Want to add a tab to show Author Archives

Hi Guys,

I would like to add an extra tab on my Buddypress profile page called "My Blogs" then when you click on it takes you to that users Author Archive Page

example link would be http://fitnessblogger.net/author/thebarebeauty/

Can anyone help with this... I have been scratching around Buddypress.org but not found a solution ??

Cheers

James

  • Alexander
    • DEV MAN’s Mascot

    Hi @FitnessBlogger,

    I've looked into this and couldn't find anything either. It might be possible with some custom coding. I can look into this further if you wouldn't mind a coding solution

    Would it be ok to simply redirect the user to that page instead of showing it below? If so, that might be easier because we can use this function: http://codex.wordpress.org/Function_Reference/get_author_posts_url

    Best regards,

  • FitnessBlogger
    • Site Builder, Child of Zeus

    Thanks for the quick reply @Alexander Rohmann

    Yes a coding solution would be great :slight_smile:

    A redirect would be fine, I have seen other plugins that add posts to the Buddypress profile page but I just want a simple link to the Author page really.

    The only problem I can see is if the user has not posted any blogs to my site you would get a 404 error ??

    example

    http://fitnessblogger.net/author/fitbyfeb/

    Any thoughts or help is really appreciated :slight_smile:

  • Alexander
    • DEV MAN’s Mascot

    Hi @FitnessBlogger,

    Got something for you!

    add_action( 'bp_setup_nav', 'add_author_link_to_bp_profile' );
    
    function add_author_link_to_bp_profile() {
        bp_core_new_nav_item( array(
            'name'                    => 'Posts',
            'slug'                    => 'author',
            'position'                => 100,
            'show_for_displayed_user' => true,
            'screen_function'         => 'redirect_user_to_author_page',
        ));
    }
    
    function redirect_user_to_author_page(){
        global $bp;
        wp_redirect( get_author_posts_url( $bp->displayed_user->id ) );
        exit;
    }

    Just add that code in your theme's functions.php or better yet, in a bp-custom.php file and you'll be good to go. I've tested this with the latest version of BuddyPress and it's working nicely.

    Let me know how it goes.

    Best regards,

  • FitnessBlogger
    • Site Builder, Child of Zeus

    Hi @Alexander Rohmann,

    Thanks for this code it works great :slight_smile:

    Is there anyway I can add a bit of code to help with the 404 error if the user has not posted any blog posts yet ??

    Also what would be really cool is to show the number of blog posts ??

    Do you think either of these might be possible ??

    Once again thank you so much for your help so far

    Kind Regards

    James

  • Alexander
    • DEV MAN’s Mascot

    Hi @FitnessBlogger,

    Give this one a try instead:

    add_action( 'bp_setup_nav', 'add_author_link_to_bp_profile' );
    
    function add_author_link_to_bp_profile() {
    
        $post_count = count_user_posts( $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',
        ));
        }
    
    }
    
    function redirect_user_to_author_page(){
        global $bp;
        wp_redirect( get_author_posts_url( $bp->displayed_user->id ) );
        exit;
    }

    This version gets a total number of posts for the user. If they actually have posts, the link will be added, but if not there won't be a link. Then the post count will be displayed as part of the link label.

    Best regards,

  • FitnessBlogger
    • Site Builder, Child of Zeus

    Hello @Alexander Rohmann

    Sorry for the delay I have been working on another project so got side tracked!!

    I have tried the new code but it does not seem to be working as mentioned ??

    Here is a user with no posts written

    http://fitnessblogger.net/frank-zocchi/

    However it shows posts (258)

    And this is the same for authors who have posted already

    http://fitnessblogger.net/rethinkhealthfitness/ (this should be just one post)

    Is there anything you think i can try to get this to work as you suggested ??

    Kind Regards

    James

  • Alexander
    • DEV MAN’s Mascot

    Hi @FitnessBlogger,

    I'm so sorry for not getting back to you here. I think the problem here is that the function I used to count posts is taking more than normal posts into account. It might even be counting the buddypress activity updates. It was working ok on my test site, but I suspect I didn't notice because it was a fresh install without enough sample data.

    Give this version a try:

    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',
        ));
        }
    
    }
    
    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 );
    }

    This will only count posts that are truly a normal WordPress post.

    Best regards,

  • FitnessBlogger
    • Site Builder, Child of Zeus

    Hello @Alexander Rohmann

    Sorry I took a while to come back on this one :slight_smile:

    I have now added the new code but still the same error happens ??

    The "posts" tab is still showing all my sites post on each user ??

    Here is a user who has not posted yest it shows (315)
    http://fitnessblogger.net/frank-zocchi/

    Ideally this user should not have the posts tab showing ??

    Then here is another user (me) and the posts number is also (315)

    http://fitnessblogger.net/fitnessblogger/

    It would be really great to get this working and I appreciate your help :slight_smile:

  • Alexander
    • DEV MAN’s Mascot

    Hi @FitnessBlogger,

    Hmm, I recall that last version was working ok on my test site, but I don't have it setup there anymore.

    Could you send me FTP info for your site as well as a WordPress admin account? I could add the code in a plugin and see what be the reason for those issues. If this is ok, just use our contact form: https://premium.wpmudev.org/contact/
    - Choose "I have a different question"
    - Include my name in the subject "Alexander Rohmann"
    - Include a link back to this thread
    - Include any relevant login information

    Best regards,

  • catalyst
    • Design Lord, Child of Thor

    There seem to have been some changes affecting the code in recent versions (after all this is an aging forum thread).

    Change the original line
    $post_count = count_user_posts_by_type( $bp->displayed_user->id );
    to this one with updated BP user ID display call:
    $post_count = count_user_posts_by_type(bp_displayed_user_id());

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.