1

I am currently using the below code to add some meta fields to my users and would love to include these in their registration email, but it appears they are not created before the email is sent.

// action hook
add_action( 'user_register', function ( $user_id ) {
            
    $pincode = wp_rand(1000,9999);

    // set pincode
    update_user_meta($user_id, 'pincode', $pincode);
 
    // update user to have pin created
    update_user_meta($user_id, 'pincreated', 1); 
    
    // set attempts to 0
    update_user_meta( $user_id, 'attempts', 0);
});

I have also now tried hooking in to the invite email, but this one didn't seem to create the pincode for me.

// action hook
function add_user_pin( $user_id ) {
     // do not set pin if user has already logged in once
    if ( get_user_meta( $user_id, 'pincreated', TRUE ) ) {
        return;
    }
    
    $pincode = wp_rand(1000,9999);

    // setpincode if logging in first time
    update_user_meta($user_id, 'pincode', $pincode);
 
    // update user to have completed first login
    update_user_meta( $user_id, 'pincreated', 1 ); 
    
    // set attempts to 0
    update_user_meta( $user_id, 'attempts', 0);
};
add_filter( 'invited_user_email', 'add_user_pin',10,4);

I have also tried another variation of below, but this doesn't seem to create the pincode at all

// action hook
add_action( 'edit_user_created_user', function ( $user_id ) {
     // do not set pin if user has already logged in once
   // if ( current_user_can( 'administrator' ) || get_user_meta( $user_id, 'pincreated', TRUE ) ) {
  //      return;
 //   }
    
    $pincode = wp_rand(1000,9999);

    // setpincode if logging in first time
    update_user_meta($user_id, 'pincode', $pincode);
 
    // update user to have completed first login
    update_user_meta( $user_id, 'pincreated', 1 ); 
    
    // set attempts to 0
    update_user_meta( $user_id, 'attempts', 0);
});
1

2 Answers 2

0

You'll need to use an earlier hook. I'd suggest new_user_email_content, which will also let you filter the contents of the email.

1
  • This seems to be related to the change of an existing user email address. I wasn't able to get it to create the pincode for my new users either.
    – Matt Holt
    Commented May 1 at 1:23
0

I was able to figure this out. You can add your custom meta to the hook specifically designed for this. insert_custom_user_meta This is then created and added to the user before the email is sent.

add_filter( 'insert_custom_user_meta', 'wp_insert_custom_user_meta_filter', 10, 4 );

function wp_insert_custom_user_meta_filter( $custom_meta, $user, $update, $userdata ){
    // Be sure to only add a new pin if creating the user by performing only when $update is false
    if (! $update) {
        $pincode = wp_rand(1000,9999);
        $custom_meta = array(
            'pincode' => $pincode,
            'pincreated' => '1',
            'attempts' => '1'
        );
        return $custom_meta;
    } else {
        return $custom_meta;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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