Archived topic
How do I link blog post authors to their BP profiles?
14 replies · Started by Nathan Pinno on December 3, 2017
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.
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;
Glad you found the solution!
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.
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/
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.
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;
}
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.
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).
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! :(
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/
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?
There's no syntax error in that example there.
What's the file look like once you add the function?
<?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...
Yea, I don't see any issues there now.