// Display Hooks For User Edit
add_action( 'show_user_profile', function( $user ) {
do_action( 'ccom_profile_fields', $user );
} );
add_action( 'edit_user_profile', function( $user ) {
do_action( 'ccom_profile_fields', $user );
} );
// Display Logic For User Edit
add_action( 'ccom_profile_fields', function( $user ) {
// Get Field Value
$ccom_custom_field = get_user_meta(
$user->ID, 'ccom_custom_field', true
);
// Show Field
printf(
'
<table class="form-table">
<tr>
<th><label for="ccom_custom_field">%s</label></th>
<td>
<input type="text" id="ccom_custom_field" name="ccom_custom_field" value="%s" />
</td>
</tr>
</table>
',
'Custom Field',
$ccom_custom_field
);
} );
// My Account Edit Profile Form
add_action( 'woocommerce_edit_account_form', function() {
// Get User Object
$user = wp_get_current_user();
?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="favorite_color">Custom Field</label>
<input type="text"
class="woocommerce-Input woocommerce-Input--text input-text"
name="ccom_custom_field" id="ccom_custom_field"
value="<?php echo esc_attr( $user->ccom_custom_field ); ?>" />
</p>
<?php
} );
// My Account Edit Profile Save
add_action( 'woocommerce_save_account_details', function( $user_id ) {
// Maybe Update Field
if( isset( $_POST['ccom_custom_field'] ) ) {
update_user_meta(
$user_id,
'ccom_custom_field',
esc_attr( $_POST['ccom_custom_field'] )
);
}
} );
// Profile Update Hooks
add_action( 'personal_options_update', function( $user_id ) {
do_action( 'ccom_profile_update', $user_id );
} );
add_action( 'edit_user_profile_update', function( $user_id ) {
do_action( 'ccom_profile_update', $user_id );
} );
// Profile Update Logic
add_action( 'ccom_profile_update', function( $user_id ) {
// Security Check
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
// Maybe Update Field
if( isset( $_POST['ccom_custom_field'] ) ) {
update_user_meta(
$user_id,
'ccom_custom_field',
esc_attr( $_POST['ccom_custom_field'] )
);
}
} );
User profile: add custom fields
Adds custom profile fields to the admin Users area and the WooCommerce My Account Edit Profile page.
How to use
- Log into a staging, development, or locally hosted clone of your site
- Install and activate Code Snippets
- WP Admin > Snippets > Add New
- Copy and paste the code from the Description tab above
- Check to ensure formatting came over properly and no syntax errors show up in the editor
- Customize the code as desired
- Add a meaningful title
- Select whether to run on front-end or back-end (or both)
- Click “Save and Activate”
- Test your site to ensure it works
- Disable if any problems, or recover
- Repeat for live environment
License
All code snippets are licensed GPLv2 (or later) matching WordPress licensing.
Disclaimer of warranty:
Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.
Support
- Describe the issue and what you’ve observed.
- Describe your expected outcome(s).
- List steps to reproduce the issue.
- Optionally provide screen-shot or video URLs.
