4
votes

I am trying to attach the billing company to the admin email. I am using this script

    add_action( 'user_register', array( $this, 'user_register' ) );
    function user_register( $user_id ) {
       // using this function to send the email
       $this->send_notification( 'admin-user', $user_id );
     }

     public function send_notification( $setting, $id ) {
     $user_company = get_user_meta($id, 'billing_company');
      wp_mail( $email, $subj, $msg.$user_company[0], $headers );
     }

the issue is that get_user_meta returns empty because according to wordpress doc when you use 'user_register' action Not all user meta data has been stored. So basically when user register the usermeta table is still empty because I tried to put an existing user id and it worked fine. https://codex.wordpress.org/Plugin_API/Action_Reference/user_register.

can anyone suggest a way to send the company name in the admin notification email?

1
Is billing_company a form field that the user has to populate? I think you still have access to $_POST at this stage so you may be able to populate $user_company based on this?Richard Parnaby-King

1 Answers

1
votes

If you are using custom registration form, you can send the email before updating the database.

 add_action( 'user_register', array( $this, 'user_register' ) );
    function user_register( $user_id ) {
       $billing_company = $_POST['billing_company'];
       wp_mail( $email, $subj, $billing_company, $headers );
    }

Alternatively, you can delay the execution of the function with sleep:

add_action( 'user_register', array( $this, 'user_register' ) );
function user_register( $user_id ) {
  sleep(5);
  $this->send_notification( 'admin-user', $user_id );
}