Skip to content

Instantly share code, notes, and snippets.

@mohsinrasool
Created July 29, 2016 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohsinrasool/add1191dbd6fae32d167ec585a2a1d09 to your computer and use it in GitHub Desktop.
Save mohsinrasool/add1191dbd6fae32d167ec585a2a1d09 to your computer and use it in GitHub Desktop.
Map WordPress user meta fields to UPME meta fields
<?php
$wp_user_query = new WP_User_Query( array( 'role' => 'Subscriber', 'fields' => 'all') );
$authors = $wp_user_query->get_results();
if (!empty($authors)) {
echo '<ul>';
$fields = array(
'Membership Category' => 'custom_field_1',
'Title' => 'custom_field_2',
'Department' => 'custom_field_3',
'Organization' => 'company',
'Address' => 'address',
'Alternate Address' => 'address2',
'City' => 'city',
'Prov' => 'state',
'PostalCode' => 'zip_code',
);
// loop through each author
foreach ($authors as $author)
{
// get all the user's data
$author_info = get_user_meta($author->ID);
// update user meta data
foreach($fields as $source => $target) {
if(!empty($author_info[$source]) && !empty($author_info[$source][0]) ){
delete_user_meta($author->ID, $target);
update_user_meta($author->ID, $target, $author_info[$source][0]);
}
}
if(!empty($author_info['AreaCode']) && !empty($author_info['AreaCode'][0])) {
delete_user_meta($author->ID, 'telephone');
update_user_meta($author->ID, 'telephone', $author_info['AreaCode'][0].'-'.$author_info['Telephone'][0]);
}
update_user_meta($author->ID, 'telephone', $author_info['AreaCode'][0].'-'.$author_info['Telephone'][0]);
$userdata = array('ID' => $author->ID);
if(!empty($author_info['Name']) && !empty($author_info['Name'][0])){
$userdata['first_name'] = $author_info['Name'][0];
$userdata['last_name'] = $author_info['Last Name'][0];
}
if(!empty($author_info['Title']) && !empty($author_info['Title'][0])){
$userdata['display_name'] = $author_info['Title'][0];
if(!empty($author_info['Organization']) && !empty($author_info['Organization'][0])){
$userdata['display_name'] .= ', '.$author_info['Organization'][0];
}
}
// update user data
wp_update_user( $userdata );
}
echo '</ul>';
} else {
echo 'No users found';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment