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.

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


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.