Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Created September 1, 2014 17:03
Show Gist options
  • Save strangerstudios/abb065260d574be46a4f to your computer and use it in GitHub Desktop.
Save strangerstudios/abb065260d574be46a4f to your computer and use it in GitHub Desktop.
Syncing user meta fields between PMPro Shipping and Event Espresso.
/*
Copy PMPro shipping address fields to event espresso when they are blank for event espresso.
Add this code to a custom plugin or your active theme's functions.php,
then visit /?metacopy=1 as an administrator.
Make sure to understand the code before running so you know it is doing what you want it to.
*/
function my_init_meta_copy()
{
if(!empty($_REQUEST['metacopy']) && current_user_can("manage_options"))
{
echo "Copying meta fields...<br />";
global $wpdb;
$user_ids = $wpdb->get_col("SELECT ID FROM $wpdb->users");
foreach($user_ids as $user_id)
{
$user = get_userdata($user_id);
echo "User " . $user->user_login . ": ";
$event_espresso_address = $user->event_espresso_address;
$pmpro_saddress1 = $user->pmpro_saddress1;
if(empty($pmpro_saddress1) && !empty($event_espresso_address))
{
//copy Event Espresso to PMPro Shipping
$um = array(
"event_espresso_address" => "pmpro_saddress1",
"event_espresso_address2" => "pmpro_saddress2",
"event_espresso_city" => "pmpro_scity",
"event_espresso_zip" => "pmpro_szipcode",
"event_espresso_state" => "pmpro_sstate",
"event_espresso_country" => "pmpro_scountry"
);
echo " Copied Event Espresso Fields to PMPro Shipping Fields.";
foreach($um as $left => $right)
{
update_user_meta($user_id, $right, get_user_meta($user_id, $left, true));
}
}
elseif(!empty($pmpro_saddress1))
{
//copy PMPro Shipping to Event Espresso
$um = array(
"pmpro_saddress1" => "event_espresso_address",
"pmpro_saddress2" => "event_espresso_address2",
"pmpro_scity" => "event_espresso_city",
"pmpro_szipcode" => "event_espresso_zip",
"pmpro_sstate" => "event_espresso_state",
"pmpro_scountry" => "event_espresso_country"
);
echo " Copied PMPro Shipping Fields to Event Espresso Fields.";
foreach($um as $left => $right)
{
update_user_meta($user_id, $right, get_user_meta($user_id, $left, true));
}
}
else
{
echo "No address found.";
}
echo "<br />";
}
exit;
}
}
add_action('init', 'my_init_meta_copy');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment