0

I created a function in function.php which send an email with user information at registration. I succeed to get the ID but I can't get custom fields... I only can get these ones : - ID - user_login - user_pass - user_nicename - user_email - user_url - user_registered - display_name

I can't either get these the user_meta like first_name or last_name. I don't understand why...

Does someone know how I can do it ? In my case, my custom field is 'code_postal'.

I show you what I have done :

function mailInscriptionSecteurRhone( $user_ID ) {
    $headers = array('Content-Type: text/html; charset=UTF-8');
    $candidat = get_userdata( $user_ID );
    $codePostalCandidat = get_field('code_postal', 'user_' . $user_ID );
    wp_mail( 'test@test.fr', 'Test', $codePostalCandidat, $headers );
}
add_action( 'user_register', 'mailInscriptionSecteurRhone', 1 );

Thank you in advance for your help :-)

enter image description here

  • Do you use some specific conditional rules to display the field? – Friss Oct 25 '18 at 19:20
1

EDIT

I think the solution given by @dboris may work. Alternatively you could try to hook your email sending after the user registration like this

global $uID; //to keep the value to reuse it later, null at this step

function mailInscriptionSecteurRhone() {
global $uID;
$user_ID = $uID;
    $headers = array('Content-Type: text/html; charset=UTF-8');
    $candidat = get_userdata( $user_ID );
    $codePostalCandidat = get_field('code_postal', 'user_' . $user_ID );
    wp_mail( 'test@test.fr', 'Test', $codePostalCandidat, $headers );
}

function trigger_mailInscriptionSecteurRhone($user_ID)
{
global $uID;
$uID = $user_ID;
   add_action( 'init', 'mailInscriptionSecteurRhone', 10 );
}
add_action( 'user_register', 'trigger_mailInscriptionSecteurRhone', 10,1 );

Instead of sending directly the email with the "not ready" data, we trigger the next init hook which is supposed to do it. We can keep the user id by using a global variable. I did not test this solution, it is a suggestion.

End edit

Ok maybe the user_register hook is triggered before acf is loaded (acc to your screenshot, you use acf)

So, have you tried get_user_meta like this ?

$codePostalCandidat = get_user_meta($user_ID,'code_postal',true);
  • Hi @Friss, thanks for you answer ! Yes I already tried it, it return an empty value... I find it on the wordpress codex: "Not all user meta data has been stored in the database when this action is triggered'. I think it come from there... – Arthur Oct 25 '18 at 20:18
0

I believe this action is triggered before the user meta has been saved. Try using $_POST variable to access the information you need, like this: $first_name = $_POST['first_name']; For the ACF custom fields, they are in array in $_POST['acf'], but indexed with their keys. You can find the key of the field by enabling it in the Field Group view in the Dashboard:

enter image description here

And then call it like this: $_POST['acf']['field_5bd2cb1f779bf']

There is currently no easy way to get this key value dynamically, except to use some custom function like this one.

  • It doesn't work. I found it in an other post but I don't understand the code. Maybe someone of you could understand it ? wordpress.stackexchange.com/questions/209400/… – Arthur Oct 26 '18 at 7:24
  • @Arthur I've seen this, but it is for altering some user meta, not getting it. I am 99% sure what you need is available in $_POST variable. Can you do this at the beginning of your function: var_dump( $_POST ); die; and share the dump with us using pastebin? – dboris Oct 26 '18 at 8:01
  • @Arthur I edited my post with the correct information about getting the values from ACF fields. For the first name and other info, the best way is to do the dump I suggested in previous comment. – dboris Oct 26 '18 at 8:24
0

@dboris @Friss Thank you a lot for your answer and the time you spent for me.

@dboris, you resolved my problem. The var_dump on the $_Post was a good idea. I found the concerned field with that method.

Result of var_dump( $_Post ) :

array(34) { ["sexe"]=> string(5) "Homme" ["user_login-115"]=> string(7) "TestTwo" ["last_name-115"]=> string(7) "TestTwo" ["first_name-115"]=> string(7) "TestTwo" ["nationalite-115"]=> string(7) "TestTwo" ["jour_naissance-115"]=> string(2) "12" ["mois_naissance"]=> string(4) "Juin" ["annee_naissance-115"]=> string(4) "1993" ["travailler-entreprise-proprete"]=> string(24) "Oui, je suis intéressé" ["annees_experience"]=> string(13) "De 0 à 5 ans" ["adresse-115"]=> string(11) "1 rue de la" ["code_postal-115"]=> string(5) "69001" ["ville-115"]=> string(6) "Lyon 1" ["user_email-115"]=> string(10) "test@tr.fr" ["email_contact-115"]=> string(10) "test@tr.fr" ["phone_number-115"]=> string(10) "0627361762" ["permis_conduire"]=> string(3) "Oui" ["vehicule"]=> string(3) "Oui" ["type_emploi"]=> string(32) "Gardien de résidence étudiante" ["date_inscription-115"]=> string(10) "28/10/2018" ["situation-couple"]=> string(12) "Célibataire" ["enfants-charge-115"]=> string(0) "" ["animaux"]=> string(3) "Oui" ["situation_actuelle"]=> string(0) "" ["experiences_dans_profession"]=> string(0) "" ["parcours_et_formation"]=> string(0) "" ["user_password-115"]=> string(10) "Ar12041997" ["confirm_user_password-115"]=> string(10) "Ar12041997" ["fichier_cv-115"]=> string(0) "" ["form_id"]=> string(3) "115" ["timestamp"]=> string(10) "1540740815" ["request"]=> string(0) "" ["_wpnonce"]=> string(10) "524409c715" ["_wp_http_referer"]=> string(37) "/gardienrecrute/inscription-candidat/" }

You saved my life !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.