Category specific email optin with WPForms

Almost all of our food blogger and publisher clients use email optins to encourage readers to join their email newsletter.

We often customize the title and description of the form based on the current post’s category. For instance, the newsletter signup form in the Beauty category might look like:

While we could build a separate form for every category, a simpler approach is to create a single form and use category metadata to override the default title and description for certain categories.

Create default form

Go to WPForms > Add New and create your newsletter signup form. Include the standard title and description that’s applicable site-wide.

If the category does not have a specific title or description set, it will use whatever you have set here.

Term Metabox for category override

Using Advanced Custom Fields, create a metabox that appears on the Edit Category screen allowing editors to change the Optin Title and Optin Description. I’m using be_optin_title and be_optin_description as the respective meta keys.

Use term meta to customize form

Finally, we can add the following code to change the form’s title and description. This code can go in your theme’s functions.php file or a core functionality plugin.

Make sure you change 123 at the top to the form ID you’d like to target. I’m using the ea_first_term() helper function to select the primary category if the post appears in more than one category.

 * Customize optin title/description by category
 * @link‎
 * @param array $form_data
 * @return array
function be_wpforms_category_optin( $form_data ) {

	if( 123 != $form_data['id'] )
		return $form_data;

	$term_id = false;

	if( is_category() )
		$term_id = get_queried_object_id();
	elseif( is_single() )
		$term_id = ea_first_term( [ 'field' => 'term_id' ] );

	if( ! $term_id )
		return $form_data;

	$title = get_term_meta( $term_id, 'be_optin_title', true );
	if( !empty( $title ) )
		$form_data['settings']['form_title'] = $title;

	$description = get_term_meta( $term_id, 'be_optin_description', true );
	if( !empty( $description ) )
		$form_data['settings']['form_desc'] = $description;

	return $form_data;
add_filter( 'wpforms_frontend_form_data', 'be_wpforms_category_optin' );

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Comments are closed. Continue the conversation with me on Twitter: @billerickson