This site is running Voxel Theme Version: 1.6.1.1 | Elementor Plugin Version: 3.30.2

Join the Voxel Guide Community!

Get Involved

Map 'First Name' and 'Last Name' fields from a Profile CPT to WordPress user meta when user update their profile

Description/Instructions

Map ‘First Name’ and ‘Last Name’ fields from a Profile CPT to WordPress user meta when user update their profile

Instructions

Create two text field in profile edit form and specify custom key
eg. 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
Copy Code

Let's Chat About this Snippet

Chat Toggle
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Ask me anything about Voxel!
Send
Powered by AI24