• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP Bitz

A curated collection of code snippets for WordPress

  • Home
  • About
  • Blog
  • Code Snippets
You are here: Home / WordPress Development Tips / Use Proofy.io API to validate WordPress registrations

Use Proofy.io API to validate WordPress registrations

WordPress Development Tips · October 24, 2022

This post brought to you by RocketGeek, ButlerBlog, and the following:

 

There are many uses for email deliverability validation, but one great (and simple) example is how to set up email validation on WordPress registrations.

I have put together a super-simple, minimum abstraction email verification API wrapper for Proofy’s email validation API that you can use for free (although you’ll need an account and API key from Proofy – but you can get that free with some starter credits – and additional blocks of checks are pretty cheap).

Get started

So, you’ve got the API wrapper (linked above) and you have a Proofy account. Let’s get started.

First, you need to initialize the object class for the API wrapper. I’d recommend doing that hooked to the WordPress init action and globalizing the object:

add_action( 'init', function() {
    // Your user ID from Proofy
    $aid = 12345;

    // Your API key from Proofy
    $api_key = 'some_random_api_key';

    // Include the api wrapper object class. (Assumes you have this file in this directory)
    require 'path/to/proofy/class-rocketgeek-proofy-email-validation.php';

    // Initiate the object class with your api key.
    global $proofy;
    $proofy = new RocketGeek_Proofy_Email_Verification_API( $aid, $api_key );
});

One thing to note with the above – make sure you have the right path to the API’s object class for the include. Otherwise, it won’t find it and things will break.

Now that the API object is initialized, you can use it anywhere (mostly). For purposes of this example, we’re going to check the validity of emails used to register through WP’s native registration process. For that, we hook into WP’s registration_errors filter so we can add an error if the email turns out to be undeliverable (i.e. fake from some jerk spammer trying to sign up to your site and steal your free stuff).

/**
 * Validates email in native WP registration using Proofy.io API.
 * 
 * @param  array  $errors               A WP_Error object containing any errors encountered during registration.
 * @param  string $sanitized_user_login User's username after it has been sanitized.
 * @param  string $user_email           User's email.
 * @return array  $errors               A WP_Error object containing any errors encountered during registration.
 */
add_filter( 'registration_errors', function( $errors, $sanitized_user_login, $user_email ) {
    
    // Assumes you have initialized the object class as global.
    global $proofy;
    
    $result = $proofy->verify( $user_email );
    
    if ( ! is_array( $result ) ) {
        $errors->add( 'email_validation', __( 'Email validation could not be completed.', 'your-text-domain' ) ); 
    } else {
        if ( 1 != $result['result'][0]['status'] ) {
            $errors->add( 'email_validation', __( 'Email is not deliverable or status is unknown.', 'your-text-domain' ) );
        }
    }
    
    return $errors;
},10, 3);

Hopefully, you can follow what’s going on in there. All we did was verify the deliverability through Proofy’s API with the wrapper’s $proofy->verify() method. If it returned a status of “1”, then the email is legit. If it is anything else, we return an error. It could be 2, 3, or 4, which would be the following:

  • 2: risky
  • 3: undeliverable
  • 4: unknown

So really, “3” is the only one we really have to return an error for, but keeping the example simple, we’re returning any of those as junk.

I hope you found this to be simple – and useful. The cleaner you keep your list, the better your email deliverability will be. And if your deliverability is kept in top shape, you shouldn’t have to worry about getting dropped or banned from your email service.

If you’d like me to write a custom application for you using this (or other) API, get started with 1-on-1 consulting at rocketgeek.com.

Filed Under: WordPress Development Tips

Chad Butler

This post brought to you by RocketGeek, ButlerBlog, and the following:

 

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Join for free!

Get free code snippets, WordPress best practices, and practical Content Marketing advice from seasoned WordPress expert Chad Butler (butlerblog):

Recent Posts

  • Bitnami WordPress Autoptimize cannot write to the cache directory
  • Create and delete WordPress sites in XAMPP with a batch file
  • How to fix “Error: MySQL Shutdown Unexpectedly” in XAMPP control panel
  • Fix missing Customizer in WordPress 6
  • Use Proofy.io API to validate WordPress registrations

Copyright © 2023 · Maker Pro on Genesis Framework · WordPress · Log in