Skip to content

Instantly share code, notes, and snippets.

@leanda
Created July 14, 2016 13:43
Show Gist options
  • Save leanda/52b799dda0560f04d13b3a8f2eb121d5 to your computer and use it in GitHub Desktop.
Save leanda/52b799dda0560f04d13b3a8f2eb121d5 to your computer and use it in GitHub Desktop.
ACF Save action hook to grab that email from member profile and update the user_email
/**
* ACF
*
* Save action hook to grab that email from member profile and update the user_email
*/
function my_acf_save_post( $post_id ) {
// bail early if no ACF data
if( empty($_POST['acf']) ) {
return;
}
// bail early if editing in admin
if( is_admin() ) {
return;
}
if( $_POST['post_id'] != 'new' ) {
$emailField = $_POST['acf']['profile_email_address'];
$wp_user_id = str_replace("user_", "", $post_id);
if (isset($emailField)) {
if (email_exists( $emailField )){
// Email exists, do not update value.
// Maybe output a warning.
update_field('profile_email_address', get_the_author_meta('user_email',$wp_user_id), $post_id);
} else {
$args = array(
'ID' => $wp_user_id,
'user_email' => esc_attr( $emailField )
);
wp_update_user( $args );
}
}
}
// return the ID
return $post_id;
}
add_action('acf/save_post', 'my_acf_save_post', 20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment