Append OptinMonster to Post Based on Tag

I started my newsletter a little less than a year ago and at the time I set it up quickly because I was just experimenting with newsletters. As I've done more research and as I've seen my list grow to 350+ subscribers without an incentive I've decided to take my opt-ins more seriously. That's why I've been playing with OptinMonster and it's actually really great.

It's not a form builder and it won't make your contact form. What it can do though is give you a really nice interface to build an opt-in form. You choose a type of opt-in (sidebar, after the post, popup, etc), define some styles, tell it when to appear (only on this page), connect it to your list and press save. It's pretty slick.

This post though isn't about opt-ins. It's about being able to choose exactly where I want to place them. I write a lot about WooCommerce. In fact, 50 of my 192 posts are about WooCommerce. And I have an newsletter for people who want to learn more about WooCommerce so it makes sense to automatically add an opt-in form to any post that's tagged woocommerce. And that's something that OptinMonster doesn't handle natively. Luckily their support was great and pointed me in the right direction.

Getting the Plugin

I've written up a little plugin that can help with this. You need to replace two values in the plugin and then you're done. Here's the plugin that you'll need. You can download the plugin from the Gist website (see the link at the bottom of the snippet) and then you'll need to open it with a code editor to modify two lines before uploading it to your site.

<?php
/*
* Plugin Name: OptinMonster Load on Tag
* Plugin URI: https://gist.github.com/BFTrick/bc0dab3881660645aaf6
* Description: Load OptinMonster Opt-ins based on the post tag.
* Author: Patrick Rauland
* Author URI: http://speakinginbytes.com
* Version: 1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class Optin_Monster_Load_On_Tag {
/
* Constructor
*/
public function __construct() {
add_filter( 'the_content', array( $this, 'load_on_tag' ), 999 );
}
/
* Load the optin based on if a tag is present
*
* @since 1.0
* @param $content the content of the post
* @return string
*/
public function load_on_tag( $content) {
// enter the tag you wish to target
$target_tag = array( 'woocommerce' );
// enter the optin slug. You can find this in the WordPress admin after clicking on OptinMonster in the menu to see a list of opt-ins. Then look for the 'Slug' column of the opt-in you wish to include
$optin_id = 'qiyzpkho8s-post';
// only load on single post pages
if ( is_single() ) {
// only load the optin on a post that has the target tag
if ( has_tag( $target_tag ) ) {
// display the optin
$content .= optin_monster_tag( $optin_id, true );
}
}
return $content;
}
}
// load after optin_monster has loaded
add_filter( 'optin_monster_init', 'load_optin_monster_load_on_tag' );
function load_optin_monster_load_on_tag() {
// make sure the function to generate the optin exists
if ( function_exists( 'optin_monster_tag' ) ) {
return new Optin_Monster_Load_On_Tag();
}
}

Editing the Tag

Once you have the plugin open with your text editor you'll notice there are two variables in the main method. The first, $target_tag, is the tag that you use to tell this plugin when to include the opt-in form. You can enter one or more tags. In my case I wanted just woocommerce so I entered:

$target_tag = array( woocommerce );

but if I wanted to target woocommerce and e-commerce I could have done it like this:

$target_tag = array( woocommerce‘, ‘e-commerce );

You can add as many tags as you want to the array. If any of them are present it will load.

Opt-in Slug

The second variable you need to edit in that same method is $optin_id. This is a bit misleading because it isn't the id we need but the slug. Luckily it's easy enough to find.

In the WordPress admin click on OptinMonster in the menu. This will load a page of all of your opt-ins. Find the opt-in you want to include at the bottom of your post and locate the slug for that opt-in. It should end with -post.

Copy that and replace qiyzpkho8s-post in the code with the slug of your opt-in.

OptinMonster Slug

You can find the opt-in slug in the WordPress admin under OptinMonster.

Disable Normal Display Options

We need to turn off the normal display options. Otherwise it will show up on your other posts.

  1. In the WordPress admin click OptinMonster
  2. Click on the opt-in you wish to include in your tagged posts. This will bring up the customizer screen where you can customize the layout and design.
  3. Click Output
    OptinMonster Customizer
  4. Leave the Enable optin on site? checked
  5. Disable the rest of the settings in that section
  6. Click Save And Exit

Upload

Alrighty. Just about done. Now upload the plugin and activate it. Boom done. That easy.

If you scroll through this site you should see the opt-in appear at the bottom of any page with a woocommerce tag.

Note: I only tested this with the after post opt-in type. This may not work with other opt-in types.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.