The show_user_profile
hook is a WordPress action hook that allows you to add custom content to the user profile page in the WordPress admin area. This hook is executed when a user profile page is being displayed, and it passes the current user’s WP_User object as a parameter.
Using this hook, you can add custom fields to the ‘Personal Options’ section of the user profile, as well as add new sections to the user profile page. This is useful if you need to collect additional user information in WordPress, or want to display custom content that is specific to your website.
Here’s an example of how you can use the show_user_profile
hook to add a custom text input field to the user profile page:
function add_custom_user_profile_field( $user ) {
?>
<h3><?php _e('Custom Field', 'your_textdomain'); ?></h3>
<table class="form-table">
<tr>
<th><label for="custom_field"><?php _e('Custom Field', 'your_textdomain'); ?></label></th>
<td>
<input type="text" name="custom_field" id="custom_field" value="<?php echo esc_attr( get_the_author_meta( 'custom_field', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e('Please enter your custom field.', 'your_textdomain'); ?></span>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'add_custom_user_profile_field' );
In this example, we are adding a custom text input field to the user profile page. The add_custom_user_profile_field
function is hooked to the show_user_profile
action hook, and it outputs the HTML markup for the custom input field. The get_the_author_meta
function is used to pre-populate the field with the current value of the custom field for the user being displayed.