Creating More Profile Fields

Because the code above works only on contact fields, you'll need to work a little harder to add fields elsewhere in the profile. In Listing 10-4, I've created a profile field for the user's birth year. You'll need to edit the code to reflect the field(s) you want to use. You can see the results in Figure 10-4.

Listing 10-4. Adding user profile fields

Plugin Name: Stephanie's Extra User Profile Fields Plugin URI:

Description: Adds an extra field (the birth year) to the user profile. Loosely based on PJW User Meta by Peter Westwood. Author: Stephanie Leary Version: 1.0

Author URI:

// Usage: the_author_meta('birth_year'); will display the birth year in templates // Show new fields in the form add_action( 'show_user_profile', 'show_more_profile_fields' ); add_action( 'edit_user_profile', 'show_more_profile_fields' );

// Save the new fields when the profile is edited add_action( 'personal_options_update', 'save_more_profile_fields' );

add_action( 'edit_user_profile_update', 'save_more_profile_fields' );

function show_more_profile_fields( $user ) { ?>

<h3>Other information</h3> <table class="form-table"> <tr>

<th><label for="birth_year"><?php _e('Birth Year'); ?></label></th> <td><input type="text" name="birth_year" id="birth_year" value="<?php echo esc_attr(get_user_meta($user->ID, 'birth_year', true) ); ?>" /></td> </tr>

function save_more_profile_fields( $user_id ) {

update_user_meta( $user_id, 'birth_year', $_POST['birth_year'], get_user_meta($user-

Figure 10-4. The added profile field

Was this article helpful?

0 0

Post a comment