Snippets / PHP
PHP
Map 'First Name' and 'Last Name' fields from a Profile CPT to WordPress user meta when user update their profile
H
HCMEDIA
·
140 views
·
Updated 1 year ago
Map 'First Name' and 'Last Name' fields from a Profile CPT to WordPress user meta when user update their profile
Create two text field in profile edit form and specify custom keyeg. vx_first_name and vx_last_name
add_action('save_post_profile', 'voxel_sync_profile_names_to_user_meta', 20, 3);
function voxel_sync_profile_names_to_user_meta($post_id, $post, $update) {
// Avoid autosave/update loops
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (wp_is_post_revision($post_id)) return;
// Make sure we're targeting the right post type
if ($post->post_type !== 'profile') return;
// Get the associated user ID from Voxel's system
$author_id = get_post_field('post_author', $post_id);
if (!$author_id) return;
// Get custom field values from Voxel (replace with correct field keys)
$first_name = get_post_meta($post_id, 'vx_first_name', true);
$last_name = get_post_meta($post_id, 'vx_last_name', true);
// Update the WordPress user meta
if (!empty($first_name)) {
update_user_meta($author_id, 'first_name', $first_name);
}
if (!empty($last_name)) {
update_user_meta($author_id, 'last_name', $last_name);
}
}
PHP · 27 lines
Discussion
Ask a question or share how you used this.
Log in
to join the discussion.
No comments yet — be the first.