Extending Visitor Conditions and passive Cache Busting

We provide the code snippets and content on this page without any guarantee or support. In most cases, these pages are created on requests or based on individual solutions sent to us by other users. After it was published, we are only reviewing the code and content when we are made aware of an issue. Please let us know if you find one.

Please only use the codes if you understand them and know how to add custom code to your WordPress site.

Advanced Ads Pro comes with passive Cache Busting, a performance-optimized method to deliver ads on cached websites. It basically performs the checks needed to choose the correct ad for a visitor using JavaScript. This includes most Visitor Conditions.

While extending Visitor Conditions with PHP-based checks is rather simple by just looking at the basic code, it is more complex to add the appropriate JavaScript code.

Find a full example of how to add a new Visitor Condition, including a JavaScript-based check for passive Cache Busting below.

Please note that this code needs adjustments. Only developers should use it, and we can’t help customizing it. If you find an obvious issue, then please let us know.

// Add new visitor condition.
add_filter( 'advanced-ads-visitor-conditions', function( $conditions ) {
    $conditions['test_condition'] = array(
        'label' => __( 'test', 'advanced-ads' ),
        'description' => __( 'test description', 'advanced-ads' ),
        'metabox' => array( 'Test_Visitor_Conditions', 'metabox' ), // callback to generate the metabox
        'check' => array( 'Test_Visitor_Conditions', 'check' ) // callback for frontend check
    );
    return $conditions;
} );

// Implement the condition options on the ad edit page
class Test_Visitor_Conditions {
    static function metabox( $options, $index = 0 ) {

	    if ( ! isset ( $options['type'] ) || '' === $options['type'] ) { return; }

    	$type_options = Advanced_Ads_Visitor_Conditions::get_instance()->conditions;

	    if ( ! isset( $type_options[ $options['type'] ] ) ) {
    	    return;
	    }

    	// form name basis
	    $name = Advanced_Ads_Visitor_Conditions::FORM_NAME . '[' . $index . ']';
    	// options
	    $value = isset( $options['value']  ) ? $options['value'] : '' ;

    	?><input type="text" name="<?php echo $name; ?>[value]" value="<?php echo $value; ?>"/>
        <input type="hidden" name="<?php echo $name; ?>[type]" value="<?php echo $options['type']; ?>"/>
        <p class="description"><?php echo $type_options[ $options['type'] ]['description']; ?></p>
        <?php
    }

	// Checks with PHP if the condition is true in the frontend when no or AJAX cache-busting is used
    static function check( $options = array() ) {
        if ( isset( $options['value'] ) && $options['value'] === 'test string' ) {
            return true;
        }

        return false;
    }

}

// Notify passive cache-busting that this condition can be checked using JavaScript.
add_filter( 'advanced-ads-js-visitor-conditions', function( $conditions ) {
    $conditions[] = 'test_condition';
    return $conditions;
} );

// Add a check using JavaScript.
add_action( 'wp_footer', function() { ?>
<script>
jQuery( document ).on( 'advads-passive-cb-conditions', function( e, Advads_passive_cb_Conditions ) {
    Advads_passive_cb_Conditions.conditions['test_condition'] = function ( options, ad ) {
        return options.value === 'test string'
    };
});
</script>
<?php
} );

Make it better

Increase your ad management skills without spending more time.
Join over 150,000 publishers and AdOpts increasing their ad revenue.