Keep On Hacking

How To Hide Opt-ins In WordPress Using jQuery?

April 28, 2017

I’ve been working on one of the WordPress projects, that uses OptinMonster a lot. If you’re new to OptinMonster, then checkout OptinMonster’s website to learn what it is and what you can do with it.

What Problem Did I face?

The client wanted to show Optins only for logged-out Users yet the Optins’ were popping up even for logged-in users. Normally, we can use Optin Monster’s output settings to hide Optins to logged-in users. But in my case, this did not happen and the Optins continued to pop up for all users.

How Did I Solve?

Having tried the display settings in the Optin Monster plugin, I found from their docs that I could use their Javascript events API to stop Optins from popping up. I plugged the following snippet only to logged-in Users using wp_enqueue_script function and Conditional tags.

$( document ).ready(function() {
    $( document ).on("OptinMonsterBeforeShow", function(a, b, c) {
        // Remove all optins whose ID begins with 'om-'
	$( "[id^='om-']" ).remove();

GitHub Gist

This snippet will be handy if you happen to deal with Optins that are real monsters.

Written by Daniel who lives and works in the beautiful city of Salem training dogs. You should follow me on Twitter

© 2020, Built with ❤️+ Gatsby