0

How can I run no-disposable-emails plugin when programmatically adding a user with wp_insert_user()?

I mean how does that work?

http://wordpress.org/plugins/no-disposable-email/

    $random_password = wp_generate_password( 8, false );

    $user_data = array(
                                    'user_login'  => $sanitized_user_login,
                                    'user_pass' => wp_generate_password ( 7, false ),
                                    'first_name'  => $first_name,
                                    'last_name' => $last_name,
                                    'user_email'  => $email,
                                    'display_name'  => $first_name . ' ' . $last_name,
                                    'nickname'  => $first_name . ' ' . $last_name,
                                    'role'    => 'TestDrive',
                                    'city'=>$city,
                                    'state'=>$state,
                                    'country'=>$country
                    );

    $user_id = wp_insert_user($user_data);

HERE what worked for me:

    $errors = new WP_Error();
    $check_disposable = no_disposable_email($errors, $sanitized_user_login, $email);
    $error_string = $check_disposable->get_error_message();

    if (!$error_string)) {
            $user_id = wp_insert_user($user_data);
    } else {
            die("Please do not use disposable email for registering!");
    }
2
  • 1
    What is var-dump($check_disposable);?
    – s_ha_dum
    Feb 13, 2014 at 17:26
  • thanks for the tip, I didn't even know about this one, but didn't need it afterall
    – zabumba
    Feb 13, 2014 at 23:53

1 Answer 1

1

What the plugin you mention does is apply a filter to registration_errors, which is used by a function called register_new_user. The quickest and easiest solution would be to use that function instead of wp_insert_user.

If that isn't possible or your code design doesn't allow it, you can always call no_disposable_email directly, and before wp_insert_user.

$errors = new WP_Error();
$sanitized_user_login = 'login';
$user_email = '[email protected]';
$check_disposable = no_disposable_email($errors, $sanitized_user_login, $user_email);
If (!is_wp_error($check_disposable)) {
  // wp_insert_user
} else {
  // whatever you need
}

Very bare-bones code sample, but should get you started.

3
  • small typo: is_wp_error instead of wp_error ... yet this condition always fall in my 'else' section. See updates in my question
    – zabumba
    Feb 13, 2014 at 17:05
  • So, you marked this "accepted". Can I ask why? your last comments make it seem like you are still having issues.
    – s_ha_dum
    Feb 13, 2014 at 22:00
  • certainly, I will update my question accordingly, basically I tested $error_string = $check_disposable->get_error_message();
    – zabumba
    Feb 13, 2014 at 23:48

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.