admin管理员组

文章数量:1122833

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);
});

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);
});
Share Improve this question edited May 7, 2024 at 3:07 Matt Holt asked Apr 30, 2024 at 3:31 Matt HoltMatt Holt 115 bronze badges 1
  • You might be looking for the edit_user_created_user hook -- it fires at the very end of the user creation process. – Pat J Commented May 1, 2024 at 18:43
Add a comment  | 

2 Answers 2

Reset to default 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.

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;
    }
}

本文标签: Include New Meta Field In New User Registration Email