At Deluxe Blog Tips, we have some guest posts from JayKrishna Yadav. In the end of each guest post, I display his name, bio and gravatar to let people know more about him. That author bio box is supported by Genesis framework, which I'm using for Deluxe Blog Tips. If you want to use the default Genesis author bio box, you have to create an user for your contributor, and fill in his bio. That's good enough unless the contributor wants to show different bio in author bio box each time he writes a post, like JayKrishna Yadav does at Deluxe Blog Tips. In this post, I'll show you how to do that with a simple meta box and a few lines of code that will replace Genesis author bio box with a custom one.
Step 1: Define meta box for guest post
To setup a meta box for guest post, I use my Meta Box plugin. The code for guest post settings is quite simple:
add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
$prefix = 'gp_'; // Guest Post
$meta_boxes[] = array(
'title' => 'Guest Post Settings',
'fields' => array(
array(
'name' => 'Name',
'id' => "{$prefix}name",
'type' => 'text',
),
array(
'name' => 'Email',
'id' => "{$prefix}email",
'type' => 'text',
),
array(
'name' => 'Bio',
'id' => "{$prefix}bio",
'type' => 'textarea',
),
),
);
return $meta_boxes;
} );
I register a meta box with 3 fields: Name (text), Email (text) and Bio (textarea). The email will be used to show author's gravatar.
If you don't know what the code means, please look at see how to create meta boxes and custom fields.
The result is:
Step 2: Replace Genesis author bio box by our custom one
Genesis has a filter genesis_author_box
that let us change the information in author bio box. I use it to display custom author bio box:
add_filter( 'genesis_author_box', 'dbt_author_box', 10, 6 );
function dbt_author_box( $text, $context, $pattern, $gravatar, $title, $description )
{
$name = rwmb_meta( 'gp_name' );
$email = rwmb_meta( 'gp_email' );
$bio = rwmb_meta( 'gp_bio' );
if ( empty( $name ) || empty( $email ) || empty( $bio ) )
return $text;
$gravatar_size = apply_filters( 'genesis_author_box_gravatar_size', 70, 'single' );
$gravatar = get_avatar( $email, $gravatar_size );
$title = apply_filters( 'genesis_author_box_title', sprintf( '<strong>%s %s</strong>', __( 'About', 'genesis' ), $name ), 'single' );
$description = wpautop( $bio );
return sprintf( $pattern, $gravatar, $title, $description );
}
In this function, I use the helper function of meta box plugin to retrieve author information, and then pass that information to a predefined $pattern
variable, which holds the HTML structure for author bio box. This makes the guest author bio box looks exactly like default author bio box.
That's all. Let me know what you think about this method in the comments below.
Bonus: I also use Revision and Meta Box plugin to create a list of contributor for each post. Read it here.
Leave a Reply