0
votes

When a new user registers on our website, they are required to fill out their company information. This information is stored in the _usermeta table with the meta_key 'company'.

All I want to do is include this information in the notification email that Wordpress sends to the site administrator. I have had some luck manipulating pluggables.php (where the default email code is located), but I can't get any meta values to send in the email.

Here is my current code:

function wp_new_user_notification($user_id, $plaintext_pass = '') {
    $user = get_userdata( $user_id );
    $user_meta = get_user_meta( $user_id );
    $company = $user_meta['company'][0];

// The blogname option is escaped with esc_html on the way into the database in sanitize_option
// we want to reverse this for the plain text arena of emails.
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

$message  = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
$message .= sprintf(__('Name: %s'), $user->display_name) . "\r\n\r\n";
$message .= sprintf(__('E-mail: %s'), $user->user_email) . "\r\n\r\n";
$message .= sprintf(__('Company: %s'), $company) . "\r\n";

@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);

The code outputs:

New user registration on your site mywebsite

Name: firstname lastname

E-mail: [email protected]

Company:

I have included get_user_meta() and get_metadata() but the value is always blank.

Any help is greatly appreciated.

1
Have you check that company is in there in database for a sp. user or not? It might be that while users are registering the company meta is not being inserted into database.Raunak Gupta
I checked this... for each id, a meta_key is generated as 'company' and a meta_value is added based on their submission e.g. 'McDonalds'fulltime hatr
ok, now use die(); before wp_mail and print get_user_meta( $user_id ) and check it is returning company or not.Raunak Gupta
you might not be pulling the data properly, Just pull the company key directly $company = get_user_meta( $user_id, 'company', true);silver
Thanks for the suggestions... pulling the company key directly had the same result and print get_user_meta( $user_id ) returned an empty value for company :/fulltime hatr

1 Answers

1
votes

I figured out the issue. The plugin I use to create new users (profile press) was posting the new user, triggering wp_new_user_notification, THEN adding custom values to the meta table. I moved the meta table function above wp_new_user_notification and the data is transferring as expected. Should anyone else run into this issue, here is how to solve it:

In wp-includes/pluggable.php, the following works as expected:

$company = get_user_meta( $user_id, 'company', true ); 
$message .= sprintf(__('Company: %s'), $company) . "\r\n";

As for profile press, navigate to wp-content/plugins/profilepress/classes/class-registration-form-auth.php and put:

// register custom profile field
if ( ! is_wp_error($user_id)) {
.
//truncated
.
            do_action('pp_after_custom_field_update', $key, $value, $user_id, 'registration');
        }
    }

above:

    if (is_int($user_id) && 'enable' == $new_user_notification) {
        wp_new_user_notification($user_id, null, 'admin');
    }

Hope this helps anyone else who has similar issues. Special thanks to @RaunakGupta for pointing me in the direction and credit to profile press for their code.