How to react to ad status changes

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.

This page contains examples of action hooks in Advanced Ads you can use to react to changes of the ad status, e.g., when publishing an ad or when it expires.

Known use cases for the filters presented here are:

  • remove an expired ad to the trash
  • send an email when an ad expires
  • refresh your site cache after an ad was published or expired

Please note that these hooks fire in the backend and frontend.

The actions fired by the hooks are just examples. You can exchange them with your own code. For further information on the parameters used by the hooks you should look them up in the source code of Advanced Ads.

<?php
/**
 * Clean WP Rocket cache if a published ad gets updated.
 *
 * @param int      $post_ID Post ID.
 * @param \WP_Post $post    Post object.
 * @param bool     $update  Whether this is an existing post being updated or not.
 */
add_action( 'save_post_' . Advanced_Ads::POST_TYPE_SLUG, function( $post_ID, $post, $update ) {
	if ( function_exists( 'rocket_clean_domain' ) && $post->post_status === 'publish' && $update ) {
		rocket_clean_domain();
	}
}, 10, 3 );Code language: PHP (php)

advanced-ads-ad-status-published

This action hook fires when an ad status changes from anything else to publish.

/**
 * Clear WP Rocket domain cache when an ad is published
 * source: https://docs.wp-rocket.me/article/494-how-to-clear-cache-via-cron-job
 */
add_action( 'advanced-ads-ad-status-published', function() {
    if ( function_exists( 'rocket_clean_domain' ) ) {
	rocket_clean_domain();
    }
});Code language: PHP (php)

advanced-ads-ad-status-unpublished

This action hook fires when an ad status changes from publish to anything else, including expiring ads.

/**
 * Clear W3 Total Cache when an ad is unpublished
 * source: https://plugins.trac.wordpress.org/browser/w3-total-cache/trunk/w3-total-cache-api.php
 */
add_action( 'advanced-ads-ad-status-unpublished', function() {
    if ( function_exists( 'w3tc_flush_all' ) ) {
	w3tc_flush_all();
    }
});Code language: PHP (php)
/**
 * Clear WP Rocket Cache when an ad is unpublished
 * source: https://docs.wp-rocket.me/article/92-rocketcleandomain
 */
add_action( 'advanced-ads-ad-status-unpublished', function() {
    if ( function_exists( 'rocket_clean_domain' ) ) {
        rocket_clean_domain();
    }
} );Code language: PHP (php)

advanced-ads-ad-status-{$old_status}-to-{$new_status}

This dynamic action hook allows you to react on any specific status changes, e.g, when the ad changes from draft to publish, you can hook into advanced-ads-ad-status-draft-to-publish.

advanced-ads-ad-expired

This hook fires when an ad expires in the frontend due to an expiry date.

/**
 * Send an email when an ad expired
 */
add_action( 'advanced-ads-ad-expired', function( $ad_id, $ad ) {
		
	$ad_id = absint( $ad_id );
	$ad_title = isset( $ad->title ) ? esc_html( $ad->title ) : false;
	
	if( ! $ad_id || ! $ad_title ) {
		return;
	}
	
	$subject = "Ad $ad_id expired. What now?";
	$message = "Your ad $ad_title expired. What would you like to do now? Check out the ad edit link below. \n";
	$message .= get_edit_post_link( $ad_id, false );
	
	wp_mail( 'yourEmail@yourDomain.com', $subject, $message );
}, 10, 2 );Code language: PHP (php)
/**
 * Clear WP Rocket Cache when an ad expired
 * source: https://docs.wp-rocket.me/article/92-rocketcleandomain
 */
add_action( 'advanced-ads-ad-expired', function() {
    if ( function_exists( 'rocket_clean_domain' ) ) {
        rocket_clean_domain();
    }
} );Code language: PHP (php)

Make it better

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