One of the interesting things to evolve out of blogging as a medium is the expansion/offshoot of the byline to the author box. The author box is generally used as a small callout with a limited biography of the author. This is the default behavior of the author box in the Genesis framework, but you can have more if you customize the Genesis author box. So let’s look at an example.
What Do We Want?
For simplicity’s sake, let’s add the author’s Twitter handle to the Genesis author box. First, we need somewhere to store the Twitter handle that’s associated with the author. WordPress makes this part simple with the user_contactmethods
filter.
add_filter( 'user_contactmethods', 'my_user_contactmethods' ) ); function my_user_contactmethods( $contacts ) { unset( $contacts[ 'yim' ] ); unset( $contacts[ 'aim' ] ); unset( $contacts[ 'jabber' ] ); $contacts[ 'twitter' ] = 'Twitter'; return $contacts; } |
In the above code, all we did was remove the virtually useless YIM, AIM, and Jabber contact methods, and add our own Twitter option. When you edit a user in the WordPress backend, you’ll now see an option to enter a Twitter URL.
When Do We Want It?
Obviously we want it when the author box shows up. Why did I even ask this question? We’ll use the genesis_author_box
filter hook to modify the author box output. There’s a couple of specific things happening here, so pay attention.
add_filter( 'genesis_author_box', 'my_genesis_author_box', 100, 6 ); function my_genesis_author_box( $output, $context, $pattern, $gravatar, $title, $description ) { $twitter = get_the_author_meta( 'twitter' ); $description .= '<br/><a href="' . $twitter . '">Find me on Twitter!</a>'.; $output = sprintf( $pattern, $gravatar, $title, $description ); return $output; } |
The first thing to notice is the priority of the filter hook. We want it to be set so it happens after the default Genesis author box hooks. I set it to 100, as that seems to be a good fit that gives us plenty of wiggle room in both directions.
Second we’ll notice that the hook function requires the HTML output in its entirety to be returned. The easiest way to accomplish this is to model the Genesis author box functions and apply the modified parts ($gravatar
, $title
, and $description
) to the known $pattern
. We’ll do this with sprintf
.
In our example, we’ve just concatenated an anchor with the Twitter URL to the description.
Now! Put It All Together
You can take our example as a whole, and drop it into your functions.php
of your Genesis-based theme. With this as your starting point, you can bring your own ideas into the author box of your theme.
<?php add_filter( 'user_contactmethods', 'my_user_contactmethods' ) ); add_filter( 'genesis_author_box', 'my_genesis_author_box', 100, 6 ); function my_user_contactmethods( $contacts ) { unset( $contacts[ 'yim' ] ); unset( $contacts[ 'aim' ] ); unset( $contacts[ 'jabber' ] ); $contacts[ 'twitter' ] = 'Twitter'; return $contacts; } function my_genesis_author_box( $output, $context, $pattern, $gravatar, $title, $description ) { $twitter = get_the_author_meta( 'twitter' ); $description .= '<br/><a href="' . $twitter . '">Find me on Twitter!</a>'.; $output = sprintf( $pattern, $gravatar, $title, $description ); return $output; } |
I wish you luck, and get creative and have fun as you customize the Genesis author box.
Thanks! This put me in on the right track for what I needed to do.
I modified your snippet to create different author boxes on single and archive pages and move the author box to the sidebar on single posts.
//* Move the author box on single posts to the sidebar
remove_action( 'genesis_after_entry', 'genesis_do_author_box_single', 8 );
add_action( 'genesis_before_sidebar_widget_area', 'genesis_do_author_box_single', 8 );
//*Add office phone to user profiles
function modify_user_contact_methods( $user_contact ) {
$user_contact['office-phone'] = __( 'Office Phone' );
return $user_contact;
}
add_filter( 'user_contactmethods', 'modify_user_contact_methods' );
//Customize the author boxes
add_filter( 'genesis_author_box', 'my_genesis_author_box', 100, 6 );
function my_genesis_author_box( $output, $context, $pattern, $gravatar, $title, $description ) {
if (is_author()){ //when we are on author archive pages
$office = get_the_author_meta( 'office-phone' );
$description = 'Office Phone: ' . $office . '' . $description ; //add the phone number before the description
$output = sprintf( $pattern, $gravatar, $title, $description );
return $output;
}
elseif (is_single()) { //when we are on single pages
$office = get_the_author_meta( 'office-phone' );
$description = 'Office Phone: ' . $office ; //remove the description and just use the phone
$output = sprintf( $pattern, $gravatar, $title, $description );
return $output;
}
}