Last active
August 29, 2015 13:58
-
-
Save martynchamberlin/10324365 to your computer and use it in GitHub Desktop.
Move data from custom Wishlist Member registration fields to WP User Meta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Just paste this code into your theme's functions.php. When the user submits the form, the | |
* input fields (specified by $fields array) goes into a bunch of cookies. Once the person is | |
* actually logged in and has a wp_users row associated with then, we then take this cookie | |
* data and associated it with their account via the user_meta API. Lastly we delete the cookies. | |
*/ | |
function move_data_from_wl_to_wp() | |
{ | |
$fields = array( 'state', 'county', 'address1', 'address2', 'city', 'zip' ); | |
$expire = time() + 60 * 60 * 24 * 30; | |
// When they initially submit the form, they are not logged in as a | |
// user. Save this data in a cookie for later | |
if ( isset( $_POST['action'] ) && $_POST['action'] == 'wpm_register' ) | |
{ | |
foreach( $fields as $field ) | |
{ | |
setcookie( 'sh_' . $field, $_POST[ $field ], $expires, '/' ); | |
} | |
setcookie( 'sh_ready_to_move', 'doh', $expires, '/' ); | |
} | |
// If they are logged in, move the data from a cookie to their user | |
// meta, and end the cookie | |
else if ( isset( $_COOKIE['sh_ready_to_move'] ) && is_user_logged_in() ) | |
{ | |
$uid = get_current_user_id(); | |
foreach ( $fields as $field ) | |
{ | |
update_user_meta( $uid, 'sh_' . $field, $_COOKIE[ 'sh_' . $field ] ); | |
// delete the cookie | |
setcookie( 'sh_' . $field, '', -1, '/' ); | |
} | |
setcookie( 'sh_ready_to_move', '', -1, '/' ); | |
} | |
} | |
move_data_from_wl_to_wp(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment