Skip to content

Instantly share code, notes, and snippets.

@bnecreative
Last active April 15, 2020 15:56
Show Gist options
  • Save bnecreative/60274555354def1788b3 to your computer and use it in GitHub Desktop.
Save bnecreative/60274555354def1788b3 to your computer and use it in GitHub Desktop.
Custom Flyout Restrictions for v1.3+
<?php // Don't copy this line
/*
* Custom Flyout Restrictions (With Example Conditional Logic)
*
* Requires BNE Flyouts v1.3
*
* With Flyouts v1.3+, you can restrict pages, post and user roles from
* the Flyout settings tab. To further extend this, you can use the filter
* bne_flyout_custom_restrictions() to add additional conditions to the mix
* as shown below. This would go in a support plugin or your theme's functions.php
*
* This is only an example function showing multiple ways you could use
* conditional logic. Use only what you need or use this as a starting point.
*
* Keep in mind, this condition can override what is found in the
* individual Flyout Settings as it comes last.
*
* @param $flyout_id The Flyout ID
* @param $skip If set to true, do not load the Flyout.
*
*/
function flyout_custom_restrictions( $skip, $flyout_id ) {
// Get the main loop and current page ID using $wp_query
// This is needed if you have to get the main page ID dynamically.
global $wp_query;
$queried_object = $wp_query->get_queried_object();
$current_page_id = ( $queried_object ) ? $queried_object->ID : null;
/*
* Example:
* Don't show any Flyouts on the front page
*/
if( is_front_page() ) {
$skip = true;
}
/*
* Example:
* Display Flyout 88 only on archive pages
*/
if( $flyout_id == '88' && is_archive() ) {
$skip = false;
} else {
$skip = true;
}
/*
* Example:
* Only show Flyout 202 everywhere except on page 195
*/
if( $flyout_id == '202' && is_page( 195 ) ) {
$skip = true;
} else {
$skip = false;
}
/*
* Example:
* Show Flyout #114 on all post of this post type
* Requires the $wp_query object above
*/
if( $flyout_id == '114' ) {
if( 'post' == get_post_type( $current_page_id ) ) {
$skip = false;
} else {
$skip = true;
}
}
/*
* Example:
*
* Disable all Flyouts everywhere, except if their custom Trigger class
* is found within the page content.
*
* Requires the $wp_query object above
*/
$skip = true;
$flyout_trigger_class = 'flyout-trigger-id-'.$flyout_id;
if( strpos( get_post_field( 'post_content', $current_page_id ), $flyout_trigger_class ) !== false) {
$skip = false;
}
/*
* Example:
* Only show Flyout 207 on WooCommerce Pages. First we need
* to check if woocommerce exist.
*/
if( function_exists( 'is_woocommerce' ) ) {
if( $flyout_id == '207' && is_woocommerce() ) {
$skip = false;
} else {
$skip = true;
}
}
/*
* Example:
* Only show Flyout 618 on a certain WooCommerce Product Category.
* First we need to check if woocommerce exist.
*/
if( function_exists( 'is_woocommerce' ) ) {
if( $flyout_id == '618' && is_woocommerce() ) {
if( is_product_category( array( 'remo-propack-multi-buy-deals' ) ) ) {
$skip = false;
} else {
$skip = true;
}
}
}
/*
* Example:
* Only show a certain Flyout only on a certain page
* and only if coming from a certain referral link.
*/
if( $flyout_id == '9' ) {
// Get the referral page url
$http_referer = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
// First set it to always hide on all pages
$skip = true;
// Setup page ID checks
$pageA = '195'; // Destination Page
$pageB = '197'; // Referral Page
// If current page is $pageA and the referral url is from $pageB, only then show the Flyout 9
if( is_page( $pageA ) && $http_referer == get_permalink( $pageB ) ) {
$skip = false;
}
} // End Flyout #9
/*
* Example:
* Only show a certain Flyout only on a certain post category
*/
// Post category slugs to check against
$cat_1 = 'category-i';
$cat_2 = 'category-ii';
// Only show this flyout on post with $cat_1
if( $flyout_id == '3519' ) {
if( !has_category( $cat_1, $current_page_id ) ) {
$skip = true;
}
}
// Only show this flyout on post with $cat_2
if( $flyout_id == '3521' ) {
if( !has_category( $cat_2, $current_page_id ) ) {
$skip = true;
}
}
/*
* Example:
* Only show Flyout ID 41 on a certain custom post type.
*
* Requires $wp_query to find the main loop of the page outside
* of the Flyout loop.
*/
if( $flyout_id == '41' ) {
// If the current page IS NOT part of this post type, skip it!
if( 'my_custom_post_type' != get_post_type( $current_page_id ) ) {
$skip = true;
}
}
/*
* Example:
* Only show Flyout ID 255 on a certain pages that include
* a slug or string in the URL.
*
*/
if( $flyout_id == '255' ) {
if( strpos( $_SERVER['REQUEST_URI'], 'my-page-slug' ) !== false ) {
$skip = false;
} else {
$skip = true;
}
}
// Must return $skip, the default setting is false.
return $skip;
}
add_filter( 'bne_flyout_custom_restrictions', 'flyout_custom_restrictions', 10, 2 );
@io-wfgh
Copy link

io-wfgh commented Jul 10, 2016

This is a working example of the complete conditional filter (example 3):

function flyout_custom_restrictions( $skip, $flyout_id ) {
    if( $flyout_id == '1020' && is_page( 3040 ) ) {
        $skip = true;
    } else {
        $skip = false;
    }
    return $skip;
}
add_filter( 'bne_flyout_custom_restrictions', 'flyout_custom_restrictions', 10, 2 );

@bnecreative
Copy link
Author

bnecreative commented Mar 30, 2017

/*
 * Example Multiple Destination/Referral possibilities:
 * Only show a certain Flyout only on certain pages
 * and only if coming from certain referral links. This
 * expands a previous example to allow multiple destinations
 * from multiple referrals.
*/
function flyout_custom_restrictions( $skip, $flyout_id ) {
	
	if( $flyout_id == '240' ) {
		
		// Get the referral page url
		$http_referer = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
		
		// First set it to always hide on all pages
		$skip = true;
		
		// Destination Pages (ID's)
		$destination_pages = array(
			'6',
			'243',
			'246',
		);
		
		// Referral Pages (URL via ID's)
		$referral_pages = array(
			get_permalink('249'),
			get_permalink('252'),
			get_permalink('255'),
		);
		
		// If current page is a $destination_pages and the referral url is from $referral_pages, only then show the Flyout 240
		if( is_page( $destination_pages ) && in_array( $http_referer, $referral_pages ) ) {
			$skip = false;
		}	
	
	} // End Flyout #240


	// Must return $skip, the default setting is false.
	return $skip;
}
add_filter( 'bne_flyout_custom_restrictions', 'flyout_custom_restrictions', 10, 2 );

@geckoseo
Copy link

geckoseo commented Aug 5, 2017

Combined example where Flyout 1709 is shown everywhere except on two pages in array.

Also added Child Theme support to check that BNE is active

if ( ! function_exists('flyout_custom_restrictions') ) {
  add_filter( 'bne_flyout_custom_restrictions', 'flyout_custom_restrictions', 10, 2 );

  function flyout_custom_restrictions( $skip, $flyout_id ) {

      if( $flyout_id == '1709') {

        $excluded_pages = array (
          230,
          1243,
      );

      if( is_page( $excluded_pages ) ) {
        $skip = true;

      } else {
          $skip = false;
      }
      return $skip;
    }
  }
}

@bnecreative
Copy link
Author

bnecreative commented Apr 15, 2020

Example for WooCommerce categories with children.

function flyout_custom_restrictions( $skip, $flyout_id ) {

	// Only this Flyout
	if( $flyout_id == '612' ) {
		
		// Initially hide for all pages
		$skip = true;
		
		// check if woo is enabled
		if( function_exists( 'is_woocommerce' ) ) {
			
			// Set the category
			$term_slug = 'clothing';
			$taxonomy = 'product_cat';
			$term_id = get_term_by( 'slug', $term_slug, $taxonomy )->term_id;
			
			// Get all children categories
			$child_ids = get_term_children( $term_id, $taxonomy );
			
			// Merge main cat with children into an array
			$terms_ids = array_merge( $child_ids, array($term_id) );
			
			// Show flyout only on these category IDs
			if( is_product_category( $terms_ids ) ) {
				$skip = false;
			}
		}
	}
	
	// Must return $skip, the default setting is false.
	return $skip;
}
add_filter( 'bne_flyout_custom_restrictions', 'flyout_custom_restrictions', 10, 2 );	

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment