Adding Functionality to Profile Filters

WordPress provides four contact methods by default: AIM, Yahoo! IM, and Jabber/Google Talk. These are, of course, extensible, meaning that you can easily add new contact methods through the use of filters. It's painless to add more, and you can even add on a little extra functionality while you're at it.

Users fill out their profile data in the WordPress Dashboard by going to UsersimageYour Profile (see Book III, Chapter 1). User profile fields are stored in the WordPress database in the user_metadata table and you can easily fetch them by using get_the_author_meta(‘aim’) and print them with the_author_meta(‘aim’). If you add a Twitter Contact Info field, it appears in profiles, and you can use the_author_meta(‘twitter’) template tags in your theme to print the account name.

Figure 7-9 shows the Twitter Contact Info field in a profile within the WordPress Dashboard.

Figure 7-9: Custom Twitter profile field, as shown in the Dashboard.

image

the_author_meta() template tag has a hook called the_author_{$field}, where the PHP variable ‘$field’ is the requested meta field assigned to each contact type in the user profile files, such as ‘aim’ in the above example. These dynamic hooks are powerful because they allow you to narrow your target.

In this example, we use the dynamic ‘the_author_twitter’ hook to change the result from “lisasabinwilson” to <a href=“http://twitter.com/lisasabinwilson“>@lisasabinwilson</a>. When you call the_author_meta(‘twitter’) in your theme, you get a clickable link to Lisa Sabin-Wilson's Twitter profile. Start by entering the following lines of code in your Theme Functions file (functions.php) in your active theme folder (add this code toward the bottom of the file before the closing ?> tag):

/**
 * Add Twitter to the list of contact methods captured via profiles.
 */
function my_add_twitter_author_meta( $contact_methods ) {
$contact_methods['twitter'] = 'Twitter';
return $contact_methods;
}

add_filter( 'user_contactmethods', 'my_add_twitter_author_meta' );

/**
 * Convert staff Twitter accounts to links to twitter.com.
 */

function my_link_author_twitter_accounts( $value ) {
if ( strlen( $value ) ) {
$url = esc_url( 'http://twitter.com/' . $value );
$value = '<a href="' . $url . '">@' . esc_html( $value ) . '</a>';
}
return $value;
}
add_filter( 'the_author_twitter', 'my_link_author_twitter_accounts' );
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.102.178