We have built some custom filters into AdSanity to help developers like yourself extend and/or modify the default behaviors. Below is a list of filters that we include in the plugin. Custom code can be placed in your theme’s functions.php file or in a custom plugin.

adsanity_ads_posts_columns #

Modifies the columns listed in the post listing on the admin.

<?php
/**
* Adds a "thumbnail" column to the AdSanity post list in the WordPress Dashboard.
*/
function example_adsanity_ads_posts_columns( $columns = array() ) {
$columns['thumbnail'] = __( 'Thumbnail', 'example' );
return $columns;
}
add_filter( 'adsanity_ads_posts_columns', 'example_adsanity_ads_posts_columns' );

adsanity_ads_sortable_posts_columns #

Modifies the list of sortable columns in the post listing on the admin.

<?php
/**
* Indicates that the "clicks" column is sortable for the AdSanity post list in the WordPress Dashboard.
*/
function example_adsanity_ads_sortable_posts_columns( $sortable_columns = array() ) {
$sortable_columns['clicks'] = 'clicks';
return $sortable_columns;
}
add_filter( 'adsanity_ads_sortable_posts_columns', 'example_adsanity_ads_sortable_posts_columns' );

adsanity_ads_posts_columns_{$column}_value #

Allows you to set the value that should be displayed in a particular column. The column is identified by a key: {$column}

<?php
/**
* Sets the value of a column called "clicks" in the AdSanity post list in the WordPress Dashboard.
*/
function example_adsanity_ads_posts_columns_clicks_value( $value = '' ) {
// Today's clicks
$clicks = get_post_meta(
get_the_ID(),
sprintf( '_clicks-%s', mktime( 0,0,0, date( 'n' ), date( 'j' ), date( 'Y' ) ) ),
true
);
if ( false === $clicks ) {
$clicks = 0;
}
return intval( $clicks );
}
add_filter( 'adsanity_ads_posts_columns_clicks_value', 'example_adsanity_ads_posts_columns_clicks_value' );

adsanity_ad_sizes #

This filter allows you to change the ad sizes that are available to choose when creating an ad unit.

<?php
/**
* Adds new responsive ad unit sizes to the selectable options when creating an ad
*/
function example_adsanity_ad_sizes( $sizes = array() ) {
$sizes['1x3'] = __( '1x3 - Responsive', 'example' );
$sizes['2x3'] = __( '2x3 - Responsive', 'example' );
return $sizes;
}
add_filter( 'adsanity_ad_sizes', 'example_adsanity_ad_sizes' );

adsanity_ads_posts_sortable_by_{$orderby} #

Allows you to set custom sorting parameters

<?php
/**
* Adds new responsive ad unit sizes to the selectable options when creating an ad
*/
function example_adsanity_ads_posts_sortable_by_clicks( $vars = array() ) {
$vars = array_merge(
$vars,
array(
'meta_key' => sprintf( '_clicks-%s', mktime( 0,0,0, date( 'n' ), date( 'j' ), date( 'Y' ) ) ),
'orderby' => 'meta_value_num'
)
);
return $vars;
}
add_filter( 'adsanity_ads_posts_sortable_by_clicks', 'example_adsanity_ads_posts_sortable_by_clicks' );

pj_ads_labels #

Allows you to change the labels of the ads custom post type.

<?php
/**
* Allows you to modify the custom post type labels for the adsa custom post type
*/
function example_pj_ads_labels( $labels = array() ) {
$labels['menu_name'] = __( 'Whitelabeled Ads', 'example' );
return $labels;
}
add_filter( 'pj_ads_labels', 'example_pj_ads_labels' );

ads_setup #

Allows you to modify any of the arguments passed to the register_post_type function

<?php
/**
* Allows you to modify any part of the custom post type registration
*
* see: https://codex.wordpress.org/Function_Reference/register_post_type
*/
function example_ads_setup( $args = array() ) {
// Change the permalink structure for the custom post type
$args['rewrite'] = array(
'slug' => 'go', // http://example.com/go/single-ad-slug
);
// Expose ads in the REST API
$args['show_in_rest'] = true;
return $args;
}
add_filter( 'ads_setup', 'example_ads_setup' );

adsanity_post_class #

Allows you to modify the css classes that wrap an ad unit

<?php
/**
* Allows you to modify the css classes that are applied to an ad unit so you can style each unit individually
*/
function example_adsanity_post_class( $classes = array(), $post = false ) {
// Adds a dynamic class that utilizes data from the post object
if ( FALSE !== $post && $post instanceof WP_Post ) {
$classes[] = sprintf( 'custom-%d', $post->ID );
}
// Adds a static class to all ads
$classes[] = 'special-ad';
return $classes;
}
add_filter( 'adsanity_post_class', 'example_adsanity_post_class', 10, 2 );

Actions #

We have built some custom actions into AdSanity to help developers like yourself extend the default behaviors. Below is a list of actions that we include in the plugin. Custom code can be placed in your theme’s functions.php file or in a custom plugin.

ads_init #

<?php
/**
* Allows you to run new code during the custom post type registration
*
* @return void
*/
function example_ads_init() {
register_taxonomy(
'ads-formats',
'ads',
array(
'labels' => array(
'name' => 'Ad Formats',
'singular_name' => 'Ad Format',
),
)
);
}
add_action( 'ads_init', 'example_ads_init' );

the ads_init function runs when the ads post type is being set up. You could use this hook to register additional taxonomies for example.

adsanity_before_ad_wrapper #

adsanity_before_ad #

adsanity_after_ad #

adsanity_after_ad_wrapper #

adsanity_before_track_click #

adsanity_before_redirect #

Ad Visibility #

All of our queries for ads use WP_Query or get_posts(), so you are able to use WordPress core hooks like pre_get_post as well as the hooks listed below.

Random Ad/Ad Group #

apply_filters( 'adsanity_hide_ad_group', $group_ids ); – Needs to return an array. $group_ids is an array of Ad Group IDs that you want included in the ad display. All ads in the included groups will be eligible for display. This is populated from the selections made in a widget, shortcode attributes, block, or template tag.

apply_filters( 'adsanity_hide_ad_in_group', array(), $group_ids ); – Needs to return an array. The array is empty by default, but you can add specific Ad Post IDs to be excluded from display. These ads would be intentionally not included in the display even if they are in the Ad Groups from the above filter.

Single Ad #

apply_filters( 'adsanity_hide_ad', false, $post_id ); – Needs to return a boolean. If true, the ad will not display. The boolean is false by default.

Customizing Ad Display #

One of the things we get asked about a lot is how to avoid detection from Ad Blockers. While we’ve explained other options to respect those visitor’s ad display preferences, we understand that there are some instances that you aren’t using AdSanity for typical advertising. Maybe you’re just using AdSanity to track views and clicks on Image based content, for example. We provide you with a couple of methods to modify the display of individual ads, groups of ads, or all ads.

Styling Ad Units #

Each Ad Unit has a number of CSS Classes and a unique HTML ID that you can target in your theme’s CSS to change its look. The format of these are:

  1. ad-{size} (example: ad-125x125) – This allows you to style all ads of the same size
  2. column-{column number} (example: column-2) – While relevant only to groups of ads, this allows you to style all ads in a particular column
  3. alignleft | aligncenter | alignright | alignnone – These four styles are generally already handled inside of most themes, but if you want to apply additional styles to better handle alignment, this is the place to do so. Note: only one class will be added to each ad unit.

If all of those classes don’t suit your needs or you want to change them, you can attach additional classes to each Ad Unit using a filter as described below.

There are a few display related actions and filters built into our templating system. These allow you to filter CSS classes and add markup around the ad unit so that you can style it.

Filters

Actions

Theme Templates #

Sometimes you need to have more control over the markup for your ads. We’ve extended WordPress Core’s template hierarchy to allow you to fully customize an ad’s markup through a theme template.

If you want to change the markup of all ads, simply copy the ad.php file from the theme-templates folder into your theme and customize away.

If you want to modify the markup for all ads of a particular size, you can rename that file to something like ad-125×125.php, where 125×125 is the size that you’re trying to modify. Note: you can find the size by looking at the classes that are applied to the ad in the markup.

Finally, if you want to change the markup of one particular ad, you can rename the file to ad-1234.php where 1234 is the ID of the ad you want to modify. Note: you can find the ID by looking at the HTML ID that is applied to the ad in the markup.

Filters

Actions

Ad Visibility

Customizing Ad Display

Didn’t find what you were looking for?

If you’re a subscriber to AdSanity and don’t find an answer to your specific question please submit a request and our support department will address your issue quickly.