Quantcast
Channel: Bits & Babble
Viewing all articles
Browse latest Browse all 11

Customize The Genesis Author Box

$
0
0

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.

Wordpress Twitter Contact Method

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.

The post Customize The Genesis Author Box appeared first on Bits & Babble.


Viewing all articles
Browse latest Browse all 11

Latest Images

Trending Articles





Latest Images