Hijacking BNFW for Email Subscriptions

A while back I wrote about using BNFW for email subscriptions. BNFW is my preferred way to send out WordPress blog posts as emails. It’s a clean and simple approach that doesn’t involve managing an email newsletter. It also nicely handles auto embeds which is not something that alternative options, like MailPoet, supports.

Email from BNFW with Twitter auto embed.

While sending out my initial announced email for my new plugin WP Freighter, I discovered a big flaw with my custom BNFW approach. None of the people on my email list could purchase my WordPress plugin! They were stuck at checkout with the following Easy Digital Downloads error.

Yikes, if people on my email listed can’t buy my product that’s sorta a deal breaker. I quickly purged my email subscriber user accounts and went on to create a solution for this particular scenario. The following is what I came up with.

Hunting through BNFW actions and filters.

BNFW has plenty of actions and filters for extending, however the core logic is based on emailing WordPress users. WordPress user accounts are precisely what I want to avoid so that my potential customers are free to purchase from my store. I also don’t force people to setup a WordPress account when subscribing to an email list. Both, buying a product and signing up for an email list, should be friction-less.

Building a custom email solution feels overwhelming, instead lets hijack the emails from BNFW.

After some trial and error attempts I came up with a workaround solution by changing the email addresses itself. Instead of creating WordPress users based on what people fill out, I decided to prepend that with something unique as to not conflict with regular WordPress users. This is managed under a custom role “Email Subscriber” as shown here.

The prepended email-subscriber_ for the login and email address happens invisibly in the background. Visitors simply signing up to receive email notifications through a Gravity Forms which then generates these email subscriber accounts on the WordPress backend.

/**
 * Generate email subscriber account with "email-subscriber_" placeholders.
 */
function wp_freighter_generate_email_subscriber( $entry, $form ) {

	$email_compare    = "email-subscriber_" . rgar( $entry, '2' );
	$user             = new WP_User;
	$user->role       = "email_subscriber";
	$user->user_login = $email_compare;
    $user->user_email = $email_compare;
    $user->first_name = $entry["1.3"];
    $user->last_name  = $entry["1.6"];
	$user_id          = wp_insert_user( $user );
	
}
add_action( 'gform_after_submission_1', 'wp_freighter_generate_email_subscriber', 10, 2 );

I use these WordPress accounts to handle subscribing and unsubscribing functionalities. Lastly, as emails are sent out, I stripe out any references to the prepended email-subscriber_ placeholder text using a wp_mail filter.

/**
 * Cleanup outgoing emails which contain "email-subscriber_" placeholders.
 */
function wp_freighter_bnfw_replace_mail( $args ) {

    if ( ! isset( $args[ "to" ] ) && strpos( $args[ "to" ], "email-subscriber_" ) !== true ) {
        return $args;
    }
    $args[ "to" ]      = str_replace( "email-subscriber_", "", $args[ "to" ] );
    $args[ "message" ] = str_replace( "email-subscriber_", "", $args[ "message" ] );
    return $args;

}
add_filter( 'wp_mail', 'wp_freighter_bnfw_replace_mail' );

The full code for my implementation of Gravity Forms, BNFW and User Role Editor can be found here. If you wish to do something similar for your WordPress site, you’ll need to modify the gform_after_submission_1 hook to match your Gravity Form ID and also the relating Gravity Form fields to match your fields.