Black Friday sale! Get up to 25% off GP Premium! Learn more ➝

[Resolved] How do I link blog post authors to their BP profiles?

Home Forums Support [Resolved] How do I link blog post authors to their BP profiles?

Home Forums Support How do I link blog post authors to their BP profiles?

  • This topic has 14 replies, 3 voices, and was last updated 6 years ago by Tom.
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #440667
    Nathan Pinno

    Hi Tom and folks,

    Can someone help me with this? I’m creating a social network (for adults only) using GP, and one of the abilities for members is the ability to blog. All blogs are shown on the front page, and I want the author names to link to their BP profiles instead of the default blog author page WordPress uses. How do I do this? I think I need to create a filter, but I forget how to do so.

    Thanks,
    Nathan Pinno

    PS. I’m using the 2.0 Beta if that helps.

    #440772
    Nathan Pinno

    If anyone’s curious, here’s the code that I found (thanks to mastershas from the BuddyPress.org support forums!):

    
    // Change Post's Author URL to Buddypress Profile URL
    add_filter('generate_post_author_output','generate_post_author_output_buddyprss_url');
    
    function generate_post_author_output_buddyprss_url( $post_author_profile_link ){
    $post_author_profile_link = sprintf( ' <span class="byline">%1$s</span>',
    			sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%1$s <a href="%2$s" title="%3$s" rel="author"><span class="author-name" itemprop="name">%4$s</span></a></span>',
    				__( 'by','generatepress'),
    				esc_url( bp_core_get_user_domain( get_the_author_meta( 'ID' ) ) ),
    				esc_attr( sprintf( __( 'Know more about %s', 'generatepress' ), get_the_author() ) ),
    				esc_html( get_the_author() )
    			)
    		) ;
    return  $post_author_profile_link;
    #440775
    Leo
    Staff
    Customer Support

    Glad you found the solution!

    #440786
    Nathan Pinno

    Okay, when I tested the code for 2.0beta3 (as that’s what the site is running on for GP), it white screened my site. Not good! Don’t know if it’s a bug, or something else. Figured I’d better report this so that it can get fixed and a solution to my problem be found.

    #440862
    Leo
    Staff
    Customer Support

    Hmm I don’t see why it would cause the white screen.

    Can you try adding it using Code Snippets? https://docs.generatepress.com/article/adding-php/

    #440865
    Nathan Pinno

    Tried using both a plugin and via a child theme. Both refused to work and gave me the infamous white screen of death. Given that this worked with 1.4 of the theme (and WP 4.8 and the latest version of BP), something must have changed.

    #440934
    Tom
    Lead Developer
    Lead Developer

    Your PHP is broken.

    It should look like this:

    add_filter('generate_post_author_output','generate_post_author_output_buddyprss_url');
    
    function generate_post_author_output_buddyprss_url( $post_author_profile_link ) {
    	$post_author_profile_link = sprintf( ' <span class="byline">%1$s</span>',
    		sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%1$s <a href="%2$s" title="%3$s" rel="author"><span class="author-name" itemprop="name">%4$s</span></a></span>',
    			__( 'by','generatepress'),
    			esc_url( bp_core_get_user_domain( get_the_author_meta( 'ID' ) ) ),
    			esc_attr( sprintf( __( 'Know more about %s', 'generatepress' ), get_the_author() ) ),
    			esc_html( get_the_author() )
    		)
    	) ;
    	
    	return  $post_author_profile_link;
    }
    #440943
    Nathan Pinno

    Tried it and still got a White Screen of Death. πŸ™ Anything else or should I give this up as a lost cause?

    I’m getting very tempted to switch to a theme with built-in BP support in the blog author profiles, and will most likely do so if we can’t find a solution for this.

    #441010
    Tom
    Lead Developer
    Lead Developer

    The only way that code will trigger a fatal error is if bp_core_get_user_domain() doesn’t exist.

    You can guard against that like this:

    add_filter('generate_post_author_output','generate_post_author_output_buddyprss_url');
    
    function generate_post_author_output_buddyprss_url( $post_author_profile_link ) {
    	if ( ! function_exists( 'bp_core_get_user_domain' ) ) {
    		return $post_author_profile_link;
    	}
    
    	$post_author_profile_link = sprintf( ' <span class="byline">%1$s</span>',
    		sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%1$s <a href="%2$s" title="%3$s" rel="author"><span class="author-name" itemprop="name">%4$s</span></a></span>',
    			__( 'by','generatepress'),
    			esc_url( bp_core_get_user_domain( get_the_author_meta( 'ID' ) ) ),
    			esc_attr( sprintf( __( 'Know more about %s', 'generatepress' ), get_the_author() ) ),
    			esc_html( get_the_author() )
    		)
    	);
    
    	return  $post_author_profile_link;
    }

    Tested the above, and it doesn’t cause any errors.

    However, if that function doesn’t exist, the function won’t work (obviously).

    #441019
    Nathan Pinno

    Worked before I added it to the child theme’s functions.php, but after I added it, I got the white screen of death still! πŸ™

    #441023
    Tom
    Lead Developer
    Lead Developer

    You’ll want to check your error_log file for a specific error – the white screen isn’t very helpful.

    It’s possible that you’re running out of PHP memory: https://docs.generatepress.com/article/increasing-php-memory-limit/

    #441027
    Nathan Pinno

    According to the log, it’s a syntax error:

    syntax error, unexpected end of file in /home/ao/public_html/wp-content/themes/generatepress-child/functions.php on line 25

    It’s also what caused the last 3 white screens of death. RAM appears to be just fine.

    The code is:

    <?php
    // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) ) exit;
    
    // BEGIN ENQUEUE PARENT ACTION
    // AUTO GENERATED - Do not modify or remove comment markers above or below:
    
    // END ENQUEUE PARENT ACTION

    So how do I fix it so that the error doesn’t pop up when I add the code to change the author link?

    #441429
    Tom
    Lead Developer
    Lead Developer

    There’s no syntax error in that example there.

    What’s the file look like once you add the function?

    #441437
    Nathan Pinno
    <?php
    // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) ) exit;
    
    // BEGIN ENQUEUE PARENT ACTION
    // AUTO GENERATED - Do not modify or remove comment markers above or below:
    
    // END ENQUEUE PARENT ACTION
    add_filter('generate_post_author_output','generate_post_author_output_buddyprss_url');
    
    function generate_post_author_output_buddyprss_url( $post_author_profile_link ) {
    	if ( ! function_exists( 'bp_core_get_user_domain' ) ) {
    		return $post_author_profile_link;
    	}
    
    	$post_author_profile_link = sprintf( ' <span class="byline">%1$s</span>',
    		sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%1$s <a href="%2$s" title="%3$s" rel="author"><span class="author-name" itemprop="name">%4$s</span></a></span>',
    			__( 'by','generatepress'),
    			esc_url( bp_core_get_user_domain( get_the_author_meta( 'ID' ) ) ),
    			esc_attr( sprintf( __( 'Know more about %s', 'generatepress' ), get_the_author() ) ),
    			esc_html( get_the_author() )
    		)
    	);
    
    	return  $post_author_profile_link;
    }

    This is what it looks like. I missed that last } though… oops!
    /me feels embarrassed…

    #441442
    Tom
    Lead Developer
    Lead Developer

    Yea, I don’t see any issues there now.

Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.