Last active
August 29, 2015 14:18
-
-
Save BFTrick/bc0dab3881660645aaf6 to your computer and use it in GitHub Desktop.
Load OptinMonster Opt-ins based on the post tag.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment