Avanzados: Filtros

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 and its add-ons. Custom code can be placed in your theme’s functions.php file or in a custom plugin. The examples use a myprefix_ prefix and myprefix text domain, so swap those for your own. Looking for actions? Check out Advanced: Actions.

Quick Reference

Visibilidad de los anuncios

These decide whether an ad shows up at all. Conditional Ad Appearance, Impressions, and User Role Ad Visibility all hook in here, and the Ordered Ad Group and Rotating Ad widgets apply them too.

# adsanity_hide_ad

Return true to hide a single ad.

Parameters

  • bool $hide: Default false.
  • int $ad_id: The ad about to be displayed.

Example

/**
 * Hides ad 123 on the home page.
 *
 * @param bool $hide  Whether to hide the ad.
 * @param int  $ad_id The ad about to be displayed.
 * @return bool
 */
function myprefix_hide_ad_on_home( $hide, $ad_id ) {
	if ( 123 === (int) $ad_id && is_front_page() ) {
		return true;
	}

	return $hide;
}
add_filter( 'adsanity_hide_ad', 'myprefix_hide_ad_on_home', 10, 2 );

# adsanity_hide_ad_group

Filters the ad group IDs about to be displayed. Remove a group from the array to hide it, or return an empty array to hide the whole unit.

Parameters

  • array $group_ids: The ad group IDs.

Example

/**
 * Hides ad group 45 from logged in users.
 *
 * @param array $group_ids The ad group IDs.
 * @return array
 */
function myprefix_hide_group_for_members( $group_ids ) {
	if ( is_user_logged_in() ) {
		$group_ids = array_values( array_diff( (array) $group_ids, array( 45 ) ) );
	}

	return $group_ids;
}
add_filter( 'adsanity_hide_ad_group', 'myprefix_hide_group_for_members' );

# adsanity_hide_ad_in_group

Return a list of ad IDs to leave out when ads are pulled from a group.

Parameters

  • array $post__not_in: Ad IDs to exclude. Default empty.
  • array $group_ids: The ad group IDs being displayed.

Example

/**
 * Leaves ads 12 and 34 out of every group on single posts.
 *
 * @param array $post__not_in Ad IDs to exclude.
 * @return array
 */
function myprefix_exclude_ads_on_posts( $post__not_in ) {
	if ( is_single() ) {
		$post__not_in = array_merge( $post__not_in, array( 12, 34 ) );
	}

	return $post__not_in;
}
add_filter( 'adsanity_hide_ad_in_group', 'myprefix_exclude_ads_on_posts' );

Ad Display & Tracking

# adsanity_post_class

Filters the CSS classes on an ad unit’s wrapper.

Parameters

  • array $classes: The CSS classes.
  • WP_Post $ad: The ad being displayed.
  • int $ad_id: The ad ID.

Example

/**
 * Adds a custom class to every ad unit.
 *
 * @param array $classes The CSS classes.
 * @return array
 */
function myprefix_ad_class( $classes ) {
	$classes[] = 'myprefix-ad';
	return $classes;
}
add_filter( 'adsanity_post_class', 'myprefix_ad_class' );

# adsanity_post_id

Filters the id attribute on an ad unit’s wrapper.

Parameters

  • string $id: Default ad-{ID}.

Example

/**
 * Changes the ID attribute on every ad unit.
 *
 * @param string $id The ID attribute.
 * @return string
 */
function myprefix_ad_id( $id ) {
	return 'sponsor-' . $id;
}
add_filter( 'adsanity_post_id', 'myprefix_ad_id' );

# adsanity_track_this

Return false to skip counting views and clicks for the current request. We use this for things like skipping bots or logged in admins.

Parameters

  • bool $track: Default true.

Example

/**
 * Doesn't count views or clicks from admins.
 *
 * @param bool $track Whether to track this request.
 * @return bool
 */
function myprefix_skip_admin_tracking( $track ) {
	if ( current_user_can( 'manage_options' ) ) {
		return false;
	}

	return $track;
}
add_filter( 'adsanity_track_this', 'myprefix_skip_admin_tracking' );

# adsanity_click_not_found_title

Filters the page title shown when someone hits an ad’s tracking URL and the ad can’t be found.

Parameters

  • string $title: Default AdSanity Error.
  • WP_Post|null $post: The current global post, if there is one.

Example

/**
 * Changes the title shown when a clicked ad can't be found.
 *
 * @return string
 */
function myprefix_not_found_title() {
	return __( 'Ad Not Found', 'myprefix' );
}
add_filter( 'adsanity_click_not_found_title', 'myprefix_not_found_title' );

# adsanity_click_not_found_message

Filters the message shown when someone hits an ad’s tracking URL and the ad can’t be found.

Parameters

  • string $message: Default You have reached this URL in error.
  • WP_Post|null $post: The current global post, if there is one.

Example

/**
 * Changes the message shown when a clicked ad can't be found.
 *
 * @return string
 */
function myprefix_not_found_message() {
	return __( 'That ad is no longer running. Head back to the home page to keep browsing.', 'myprefix' );
}
add_filter( 'adsanity_click_not_found_message', 'myprefix_not_found_message' );

Post Type & Ad Sizes

# ads_setup

Filters the arguments passed to register_post_type() for the ads post type.

Parameters

  • array $args: The post type arguments.

Example

/**
 * Shows ads in the site's search results.
 *
 * @param array $args The post type arguments.
 * @return array
 */
function myprefix_searchable_ads( $args ) {
	$args['exclude_from_search'] = false;
	return $args;
}
add_filter( 'ads_setup', 'myprefix_searchable_ads' );

# pj_ad_labels

Filters the labels for the ads post type.

Parameters

  • array $labels: The post type labels.

Example

/**
 * Renames ads to sponsors.
 *
 * @param array $labels The post type labels.
 * @return array
 */
function myprefix_ad_labels( $labels ) {
	$labels['name']          = __( 'Sponsors', 'myprefix' );
	$labels['singular_name'] = __( 'Sponsor', 'myprefix' );
	$labels['add_new_item']  = __( 'Add New Sponsor', 'myprefix' );
	return $labels;
}
add_filter( 'pj_ad_labels', 'myprefix_ad_labels' );

# adsanity_ad_sizes

Filters the ad sizes you can choose from when creating an ad.

Parameters

  • array $sizes: Sizes keyed by dimensions (like 300x250) with a label as the value.

Example

/**
 * Adds a billboard ad size.
 *
 * @param array $sizes The available ad sizes.
 * @return array
 */
function myprefix_ad_sizes( $sizes ) {
	$sizes['970x250'] = __( 'Billboard (970x250)', 'myprefix' );
	return $sizes;
}
add_filter( 'adsanity_ad_sizes', 'myprefix_ad_sizes' );

# adsanity_ad_size_default

Filters the size that’s selected by default on a new ad.

Parameters

  • string $size: Default 300x250.

Example

/**
 * Selects the leaderboard size by default on new ads.
 *
 * @return string
 */
function myprefix_default_ad_size() {
	return '728x90';
}
add_filter( 'adsanity_ad_size_default', 'myprefix_default_ad_size' );

Ads List & Ad Editor

# adsanity_ads_posts_columns

Filters the columns in the ads list in the admin.

Parameters

  • array $columns: Column labels keyed by column ID.

Example

/**
 * Adds an Advertiser column to the ads list.
 *
 * @param array $columns Column labels keyed by column ID.
 * @return array
 */
function myprefix_advertiser_column( $columns ) {
	$columns['myprefix-advertiser'] = __( 'Advertiser', 'myprefix' );
	return $columns;
}
add_filter( 'adsanity_ads_posts_columns', 'myprefix_advertiser_column' );

# adsanity_ads_sortable_posts_columns

Filters which columns in the ads list can be sorted.

Parameters

  • array $columns: Sort keys keyed by column ID.

Example

/**
 * Makes the Advertiser column sortable.
 *
 * @param array $columns Sort keys keyed by column ID.
 * @return array
 */
function myprefix_sortable_advertiser_column( $columns ) {
	$columns['myprefix-advertiser'] = 'myprefix_advertiser';
	return $columns;
}
add_filter( 'adsanity_ads_sortable_posts_columns', 'myprefix_sortable_advertiser_column' );

# adsanity_ads_posts_columns_{$column}_value

Filters the output of a single column in the ads list. Use it to fill in a column you added with adsanity_ads_posts_columns.

Parameters

  • string $value: The column’s output.
  • string $column: The column ID.
  • int $post_id: The ad ID.

Example

/**
 * Fills the Advertiser column added with adsanity_ads_posts_columns.
 *
 * @param string $value   The column's output.
 * @param string $column  The column ID.
 * @param int    $post_id The ad ID.
 * @return string
 */
function myprefix_advertiser_column_value( $value, $column, $post_id ) {
	return esc_html( get_post_meta( $post_id, 'myprefix_advertiser', true ) );
}
add_filter( 'adsanity_ads_posts_columns_myprefix-advertiser_value', 'myprefix_advertiser_column_value', 10, 3 );

# adsanity_ads_posts_sortable_by_{$orderby}

Filters the query vars when the ads list is sorted by a given column. Use it to tell WordPress how to sort a column you made sortable.

Parameters

  • array $vars: The request query vars.

Example

/**
 * Sorts by the myprefix_advertiser key added with adsanity_ads_sortable_posts_columns.
 *
 * @param array $vars The request query vars.
 * @return array
 */
function myprefix_sort_by_advertiser( $vars ) {
	$vars['meta_key'] = 'myprefix_advertiser'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Sorting by meta needs a meta key.
	$vars['orderby']  = 'meta_value';
	return $vars;
}
add_filter( 'adsanity_ads_posts_sortable_by_myprefix_advertiser', 'myprefix_sort_by_advertiser' );

# adsanity_ad_edit_tabs

Filters the ad type tabs on the ad edit screen and the fields each tab shows.

Parameters

  • array $tabs: The tabs.

Example

/**
 * Renames the Text Ad tab.
 *
 * @param array $tabs The ad type tabs.
 * @return array
 */
function myprefix_ad_edit_tabs( $tabs ) {
	$tabs['text']['label'] = __( 'Sponsored Text', 'myprefix' );
	return $tabs;
}
add_filter( 'adsanity_ad_edit_tabs', 'myprefix_ad_edit_tabs' );

# adsanity_ad_edit_active_tab

Filters which ad type tab is selected when the ad edit screen loads.

Parameters

  • string $active_tab: The selected tab.
  • array $tabs: The tabs.
  • WP_Post $post: The ad being edited.

Example

/**
 * Starts new ads on the External Ad Network tab.
 *
 * @param string  $active_tab The selected tab.
 * @param array   $tabs       The ad type tabs.
 * @param WP_Post $post       The ad being edited.
 * @return string
 */
function myprefix_default_ad_tab( $active_tab, $tabs, $post ) {
	if ( 'auto-draft' === $post->post_status ) {
		return 'external';
	}

	return $active_tab;
}
add_filter( 'adsanity_ad_edit_active_tab', 'myprefix_default_ad_tab', 10, 3 );

Ajustes

# adsanity-addons

Registers a tab under Settings > Add-ons. Note that this one uses hyphens instead of underscores.

Parameters

  • array $addons: Each item is an array with a slug and a name.

Example

/**
 * Adds a settings tab that shows the adsanity-my-addon-options settings.
 *
 * @param array $addons The registered add-on tabs.
 * @return array
 */
function myprefix_addon_tab( $addons ) {
	$addons[] = array(
		'slug' => 'my-addon',
		'name' => __( 'My Add-on', 'myprefix' ),
	);
	return $addons;
}
add_filter( 'adsanity-addons', 'myprefix_addon_tab' );

# adsanity_save_admin_options

Filters the AdSanity settings right before they’re saved. Use it to sanitize and save any settings you’ve added.

Parameters

  • array $clean: The sanitized settings.
  • array $dirty: The raw submitted settings.

Example

/**
 * Saves the Heading field added with adsanity_automatic_inclusion_after_fields.
 *
 * @param array $clean The sanitized settings.
 * @param array $dirty The raw submitted settings.
 * @return array
 */
function myprefix_save_rule_heading( $clean, $dirty ) {
	if ( empty( $dirty['adsanity_in_content_rules'] ) ) {
		return $clean;
	}

	foreach ( $dirty['adsanity_in_content_rules'] as $key => $rule ) {
		if ( isset( $rule['myprefix_heading'], $clean['adsanity_in_content_rules'][ $key ] ) ) {
			$clean['adsanity_in_content_rules'][ $key ]['myprefix_heading'] = sanitize_text_field( $rule['myprefix_heading'] );
		}
	}

	return $clean;
}
add_filter( 'adsanity_save_admin_options', 'myprefix_save_rule_heading', 10, 2 );

Automatic Inclusion

# adsanity_automatic_inclusion_types

Filters the display types you can choose for an Automatic Inclusion rule. The type field only shows up when there’s more than one option. The Rotating Ad Widget add-on adds its rotating type here.

Parameters

  • array $types: Default [ 'random_ad' => 'Random Ad' ].

Example

/**
 * Adds a custom Automatic Inclusion type.
 *
 * @param array $types The available types.
 * @return array
 */
function myprefix_inclusion_types( $types ) {
	$types['myprefix_ad'] = __( 'My Custom Ad', 'myprefix' );
	return $types;
}
add_filter( 'adsanity_automatic_inclusion_types', 'myprefix_inclusion_types' );

# adsanity_automatic_inclusion_template_tag

Filters the function that renders an Automatic Inclusion rule’s ad.

Parameters

  • callable $template_tag: Default adsanity_show_ad_group.
  • array $rule: The rule’s saved settings.

Example

/**
 * Renders "My Custom Ad" rules with a custom function.
 *
 * @param callable $template_tag The function that renders the ad.
 * @param array    $rule         The rule's saved settings.
 * @return callable
 */
function myprefix_inclusion_template_tag( $template_tag, $rule ) {
	if ( isset( $rule['type'] ) && 'myprefix_ad' === $rule['type'] ) {
		return 'myprefix_show_ad';
	}

	return $template_tag;
}
add_filter( 'adsanity_automatic_inclusion_template_tag', 'myprefix_inclusion_template_tag', 10, 2 );

# adsanity_automatic_inclusion_args

Filters the arguments passed to that function.

Parameters

  • array $args: The template tag arguments.
  • array $rule: The rule’s saved settings.

Example

/**
 * Passes the Heading field from adsanity_automatic_inclusion_after_fields along.
 *
 * @param array $args The template tag arguments.
 * @param array $rule The rule's saved settings.
 * @return array
 */
function myprefix_inclusion_args( $args, $rule ) {
	if ( ! empty( $rule['myprefix_heading'] ) ) {
		$args['heading'] = $rule['myprefix_heading'];
	}

	return $args;
}
add_filter( 'adsanity_automatic_inclusion_args', 'myprefix_inclusion_args', 10, 2 );

# adsanity_automatic_inclusion_ignored_post_types

Filters the post types left out of the Automatic Inclusion post type list.

Parameters

  • array $post_types: Default ads, attachment, and wp_block.

Example

/**
 * Leaves products out of the Automatic Inclusion post type list.
 *
 * @param array $post_types The ignored post types.
 * @return array
 */
function myprefix_ignored_post_types( $post_types ) {
	$post_types[] = 'product';
	return $post_types;
}
add_filter( 'adsanity_automatic_inclusion_ignored_post_types', 'myprefix_ignored_post_types' );

# adsanity_ignore_blocks_in_count

Filters the blocks that don’t count when placing an ad before or after a number of blocks.

Parameters

  • array $blocks: Default core/column and core/columns.

Example

/**
 * Doesn't count spacers or separators when placing ads between blocks.
 *
 * @param array $blocks The ignored block names.
 * @return array
 */
function myprefix_ignored_blocks( $blocks ) {
	$blocks[] = 'core/spacer';
	$blocks[] = 'core/separator';
	return $blocks;
}
add_filter( 'adsanity_ignore_blocks_in_count', 'myprefix_ignored_blocks' );

Ad Queries

# adsanity_get_ads_args

Filters the get_posts() arguments AdSanity uses to look up ads.

Parameters

  • array $args: The query arguments.

Example

/**
 * Only returns published ads.
 *
 * @param array $args The query arguments.
 * @return array
 */
function myprefix_published_ads_only( $args ) {
	$args['post_status'] = 'publish';
	return $args;
}
add_filter( 'adsanity_get_ads_args', 'myprefix_published_ads_only' );

# adsanity_get_all_ads_args

Filters the arguments used when AdSanity looks up every ad (in reports, for example).

Parameters

  • array $args: The query arguments.

Example

/**
 * Sorts ads alphabetically in reports.
 *
 * @param array $args The query arguments.
 * @return array
 */
function myprefix_sort_all_ads( $args ) {
	$args['orderby'] = 'title';
	$args['order']   = 'ASC';
	return $args;
}
add_filter( 'adsanity_get_all_ads_args', 'myprefix_sort_all_ads' );

# adsanity_attach_meta

Filters the meta data attached to each ad object returned by those lookups.

Parameters

  • array $meta: The ad’s meta data.

Example

/**
 * Leaves internal notes off the attached meta.
 *
 * @param array $meta The ad's meta data.
 * @return array
 */
function myprefix_attach_meta( $meta ) {
	unset( $meta['myprefix_internal_notes'] );
	return $meta;
}
add_filter( 'adsanity_attach_meta', 'myprefix_attach_meta' );

Informes

# adsanity_reports_tabs

Filters the tabs on the Reports screen. Pair it with adsanity_reports_view to add your own report.

Parameters

  • array $tabs: Tab labels keyed by slug.

Example

/**
 * Adds a custom tab to the Reports screen.
 *
 * @param array $tabs Tab labels keyed by slug.
 * @return array
 */
function myprefix_reports_tab( $tabs ) {
	$tabs['my-report'] = __( 'My Report', 'myprefix' );
	return $tabs;
}
add_filter( 'adsanity_reports_tabs', 'myprefix_reports_tab' );

# adsanity_reports_active_tab

Filters which Reports tab is selected.

Parameters

  • string $active_tab: The selected tab slug.
  • array $tabs: The tabs.

Example

/**
 * Selects the custom report tab when it's requested.
 *
 * @param string $active_tab The selected tab slug.
 * @return string
 */
function myprefix_reports_active_tab( $active_tab ) {
	$tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only used to pick a tab.

	if ( 'my-report' === $tab ) {
		return 'my-report';
	}

	return $active_tab;
}
add_filter( 'adsanity_reports_active_tab', 'myprefix_reports_active_tab' );

# adsanity_reports_view

Return the full path to a PHP template to render a custom Reports tab.

Parameters

  • string $template: Default empty.
  • string $active_tab: The selected tab slug.
  • array $tabs: The tabs.

Example

/**
 * Loads the template for the custom report tab.
 *
 * @param string $template   The template path.
 * @param string $active_tab The selected tab slug.
 * @return string
 */
function myprefix_reports_view( $template, $active_tab ) {
	if ( 'my-report' === $active_tab ) {
		return __DIR__ . '/views/my-report.php';
	}

	return $template;
}
add_filter( 'adsanity_reports_view', 'myprefix_reports_view', 10, 2 );

# adsanity_override_total_stats_transient

Return true to skip the cached totals on the Reports dashboard and calculate them fresh.

Parameters

  • bool $override: Default false.

Example

/**
 * Always calculates fresh dashboard totals for admins.
 *
 * @param bool $override Whether to skip the cached totals.
 * @return bool
 */
function myprefix_fresh_totals_for_admins( $override ) {
	return current_user_can( 'manage_options' ) ? true : $override;
}
add_filter( 'adsanity_override_total_stats_transient', 'myprefix_fresh_totals_for_admins' );

# adsanity_total_stats_args

Filters the get_posts() arguments used to gather ads for the dashboard totals.

Parameters

  • array $args: The query arguments.

Example

/**
 * Only counts published ads in the dashboard totals.
 *
 * @param array $args The query arguments.
 * @return array
 */
function myprefix_total_stats_args( $args ) {
	$args['post_status'] = 'publish';
	return $args;
}
add_filter( 'adsanity_total_stats_args', 'myprefix_total_stats_args' );

# adsanity_show_ad_group_block

Return false to hide the ad group stats block on the dashboard. This also applies on the Google Analytics Tracking reports screen.

Parameters

  • bool $show: Default true.

Example

add_filter( 'adsanity_show_ad_group_block', '__return_false' );

# adsanity_get_ads_by_term_row

Filters a single row in the ad group stats table.

Parameters

  • array $row: The row, with link, views, clicks, and ctr keys.
  • int $ad_id: The ad ID.

Example

/**
 * Links each group stats row to the ad on the front end instead of the editor.
 *
 * @param array $row   The row.
 * @param int   $ad_id The ad ID.
 * @return array
 */
function myprefix_group_row_link( $row, $ad_id ) {
	$row['link'] = get_permalink( $ad_id );
	return $row;
}
add_filter( 'adsanity_get_ads_by_term_row', 'myprefix_group_row_link', 10, 2 );

# adsanity_get_ads_by_term_rows

Filters every row in the ad group stats table before they’re sorted by CTR.

Parameters

  • array $rows: The rows, keyed by ad title.

Example

/**
 * Only shows the first 10 ads in each group.
 *
 * @param array $rows The rows, keyed by ad title.
 * @return array
 */
function myprefix_limit_group_rows( $rows ) {
	return array_slice( $rows, 0, 10, true );
}
add_filter( 'adsanity_get_ads_by_term_rows', 'myprefix_limit_group_rows' );

# adsanity_custom_reports_max_results

Filters how many ads you can include in a single Custom Report.

Parameters

  • int $max: Default 15.

Example

/**
 * Allows up to 30 ads in a Custom Report.
 *
 * @return int
 */
function myprefix_custom_report_max() {
	return 30;
}
add_filter( 'adsanity_custom_reports_max_results', 'myprefix_custom_report_max' );

# adsanity_custom_report_table_cell

Filters the cells in a Custom Reports row. Add a <td> here to fill a column you added with the adsanity_custom_reports_table_heading action.

Parameters

  • array $row: The row’s HTML, one item per cell.
  • int $ad_id: The ad ID.
  • int $timestamp: The day this row covers.

Example

/**
 * Fills the Ad Size column added with adsanity_custom_reports_table_heading.
 *
 * @param array $row   The row's HTML, one item per cell.
 * @param int   $ad_id The ad ID.
 * @return array
 */
function myprefix_custom_report_cell( $row, $ad_id ) {
	$row[] = sprintf( '<td>%s</td>', esc_html( get_post_meta( $ad_id, '_size', true ) ) );
	return $row;
}
add_filter( 'adsanity_custom_report_table_cell', 'myprefix_custom_report_cell', 10, 2 );

# adsanity_export_stats_header_row_data

Filters the header row of the CSV stats export.

Parameters

  • array $row: The column headings.

Example

/**
 * Adds an Ad Size column to the CSV export.
 *
 * @param array $row The column headings.
 * @return array
 */
function myprefix_export_header( $row ) {
	$row[] = __( 'Ad Size', 'myprefix' );
	return $row;
}
add_filter( 'adsanity_export_stats_header_row_data', 'myprefix_export_header' );

# adsanity_export_stats_data_row_data

Filters each data row of the CSV stats export.

Parameters

  • array $row: The row values.
  • int $timestamp: The day this row covers.
  • WP_Post $ad: The ad.

Example

/**
 * Fills the Ad Size column in the CSV export.
 *
 * @param array   $row       The row values.
 * @param int     $timestamp The day this row covers.
 * @param WP_Post $ad        The ad.
 * @return array
 */
function myprefix_export_row( $row, $timestamp, $ad ) {
	$row[] = get_post_meta( $ad->ID, '_size', true );
	return $row;
}
add_filter( 'adsanity_export_stats_data_row_data', 'myprefix_export_row', 10, 3 );

# adsanity_export_stats_totals_row_data

Filters the totals row of the CSV stats export.

Parameters

  • array $row: The row values.
  • array $table_data: Every row above it.
  • int $total_views: Total views.
  • int $total_clicks: Total clicks.

Example

/**
 * Keeps the totals row lined up with the extra Ad Size column.
 *
 * @param array $row The row values.
 * @return array
 */
function myprefix_export_totals( $row ) {
	$row[] = '';
	return $row;
}
add_filter( 'adsanity_export_stats_totals_row_data', 'myprefix_export_totals' );

Ad Meta Data

These let you store or read AdSanity’s ad data somewhere other than the WordPress database. Like the meta data actions, they only apply to data that goes through AdSanity (including every tracked view and click, so keep them light), and the dynamic versions use the stored meta key (so _size becomes adsanity_pre_get_meta__size).

# adsanity_pre_get_meta

Supply a value before AdSanity reads from the database. If both your value and the database value are arrays, they’re merged. Otherwise a non-empty value from you wins.

Parameters

  • mixed $value: Default empty.
  • string $object_type: The object type, usually post.
  • int $object_id: The object ID.
  • string $meta_key: The meta key.
  • bool $single: Whether a single value was requested.

Example

/**
 * Supplies _myprefix_remote_data from an option instead of post meta.
 *
 * @param mixed  $value       The value so far.
 * @param string $object_type The object type.
 * @param int    $object_id   The object ID.
 * @param string $meta_key    The meta key.
 * @return mixed
 */
function myprefix_pre_get_remote_data( $value, $object_type, $object_id, $meta_key ) {
	if ( '_myprefix_remote_data' === $meta_key ) {
		return get_option( 'myprefix_remote_data_' . $object_id, $value );
	}

	return $value;
}
add_filter( 'adsanity_pre_get_meta', 'myprefix_pre_get_remote_data', 10, 4 );

# adsanity_pre_get_meta_{$meta_key}

Same as above, for a specific meta key.

Parameters

  • mixed $value: The value so far.
  • string $object_type: The object type, usually post.
  • int $object_id: The object ID.
  • bool $single: Whether a single value was requested.

Example

/**
 * Uses a campaign URL instead of the saved ad URL (_url) when one is set.
 *
 * @param mixed  $value       The value so far.
 * @param string $object_type The object type.
 * @param int    $object_id   The object ID.
 * @return mixed
 */
function myprefix_campaign_url( $value, $object_type, $object_id ) {
	$campaign_url = get_post_meta( $object_id, 'myprefix_campaign_url', true );
	return $campaign_url ? $campaign_url : $value;
}
add_filter( 'adsanity_pre_get_meta__url', 'myprefix_campaign_url', 10, 3 );

# adsanity_disable_local_get_meta

Return true to skip the database entirely and use the value from adsanity_pre_get_meta.

Parameters

  • bool $disable: Default false.
  • mixed $value: The empty default value.
  • string $object_type: The object type, usually post.
  • int $object_id: The object ID.
  • string $meta_key: The meta key.
  • bool $single: Whether a single value was requested.

Example

/**
 * Skips the database for _myprefix_remote_data and uses the value from adsanity_pre_get_meta.
 *
 * @param bool   $disable     Whether to skip the database.
 * @param mixed  $value       The empty default value.
 * @param string $object_type The object type.
 * @param int    $object_id   The object ID.
 * @param string $meta_key    The meta key.
 * @return bool
 */
function myprefix_skip_remote_data_get( $disable, $value, $object_type, $object_id, $meta_key ) {
	return '_myprefix_remote_data' === $meta_key ? true : $disable;
}
add_filter( 'adsanity_disable_local_get_meta', 'myprefix_skip_remote_data_get', 10, 5 );

# adsanity_disable_local_get_{$meta_key}_meta

Same as above, for a specific meta key.

Parameters

  • bool $disable: Default false.
  • mixed $value: The empty default value.
  • string $object_type: The object type, usually post.
  • int $object_id: The object ID.
  • bool $single: Whether a single value was requested.

Example

add_filter( 'adsanity_disable_local_get__myprefix_remote_data_meta', '__return_true' );

# adsanity_after_get_meta

Filters the value after it’s been read from the database.

Parameters

  • mixed $value: The stored value.
  • string $object_type: The object type, usually post.
  • int $object_id: The object ID.
  • string $meta_key: The meta key.
  • bool $single: Whether a single value was requested.

Example

/**
 * Forces ad URLs to https.
 *
 * @param mixed  $value       The stored value.
 * @param string $object_type The object type.
 * @param int    $object_id   The object ID.
 * @param string $meta_key    The meta key.
 * @param bool   $single      Whether a single value was requested.
 * @return mixed
 */
function myprefix_https_ad_urls( $value, $object_type, $object_id, $meta_key, $single ) {
	if ( '_url' === $meta_key && $single ) {
		return set_url_scheme( $value, 'https' );
	}

	return $value;
}
add_filter( 'adsanity_after_get_meta', 'myprefix_https_ad_urls', 10, 5 );

# adsanity_after_get_meta_{$meta_key}

Same as above, for a specific meta key.

Parameters

  • mixed $value: The stored value.
  • string $object_type: The object type, usually post.
  • int $object_id: The object ID.
  • bool $single: Whether a single value was requested.

Example

/**
 * Forces every ad URL (_url) to https.
 *
 * @param mixed  $value       The stored value.
 * @param string $object_type The object type.
 * @param int    $object_id   The object ID.
 * @param bool   $single      Whether a single value was requested.
 * @return mixed
 */
function myprefix_https_ad_url( $value, $object_type, $object_id, $single ) {
	return $single ? set_url_scheme( $value, 'https' ) : $value;
}
add_filter( 'adsanity_after_get_meta__url', 'myprefix_https_ad_url', 10, 4 );

# adsanity_disable_local_update_meta

Return true to skip writing a value to the database (if you’re saving it somewhere else).

Parameters

  • bool $disable: Default false.
  • string $object_type: The object type, usually post.
  • int $object_id: The object ID.
  • string $meta_key: The meta key.
  • mixed $meta_value: The value being saved or removed.
  • mixed $prev_value: The previous value to replace, if one was given.

Example

/**
 * Saves _myprefix_remote_data to an option instead of post meta.
 *
 * @param bool   $disable     Whether to skip the database.
 * @param string $object_type The object type.
 * @param int    $object_id   The object ID.
 * @param string $meta_key    The meta key.
 * @param mixed  $meta_value  The value being saved.
 * @return bool
 */
function myprefix_save_remote_data( $disable, $object_type, $object_id, $meta_key, $meta_value ) {
	if ( '_myprefix_remote_data' !== $meta_key ) {
		return $disable;
	}

	update_option( 'myprefix_remote_data_' . $object_id, $meta_value );
	return true;
}
add_filter( 'adsanity_disable_local_update_meta', 'myprefix_save_remote_data', 10, 5 );

# adsanity_disable_local_update_{$meta_key}_meta

Same as above, for a specific meta key.

Parameters

  • bool $disable: Default false.
  • string $object_type: The object type, usually post.
  • int $object_id: The object ID.
  • mixed $meta_value: The value being saved or removed.
  • mixed $prev_value: The previous value to replace, if one was given.

Example

/**
 * Saves _myprefix_remote_data to an option instead of post meta.
 *
 * @param bool   $disable     Whether to skip the database.
 * @param string $object_type The object type.
 * @param int    $object_id   The object ID.
 * @param mixed  $meta_value  The value being saved.
 * @return bool
 */
function myprefix_save_remote_data_meta( $disable, $object_type, $object_id, $meta_value ) {
	update_option( 'myprefix_remote_data_' . $object_id, $meta_value );
	return true;
}
add_filter( 'adsanity_disable_local_update__myprefix_remote_data_meta', 'myprefix_save_remote_data_meta', 10, 4 );

# adsanity_disable_local_delete_meta

Return true to skip deleting a value from the database.

Parameters

  • bool $disable: Default false.
  • string $object_type: The object type, usually post.
  • int $object_id: The object ID.
  • string $meta_key: The meta key.
  • mixed $meta_value: The value being saved or removed.
  • bool $delete_all: Whether matching meta is being deleted from every object.

Example

/**
 * Deletes _myprefix_remote_data from an option instead of post meta.
 *
 * @param bool   $disable     Whether to skip the database.
 * @param string $object_type The object type.
 * @param int    $object_id   The object ID.
 * @param string $meta_key    The meta key.
 * @return bool
 */
function myprefix_delete_remote_data( $disable, $object_type, $object_id, $meta_key ) {
	if ( '_myprefix_remote_data' !== $meta_key ) {
		return $disable;
	}

	delete_option( 'myprefix_remote_data_' . $object_id );
	return true;
}
add_filter( 'adsanity_disable_local_delete_meta', 'myprefix_delete_remote_data', 10, 4 );

# adsanity_disable_local_delete_{$meta_key}_meta

Same as above, for a specific meta key.

Parameters

  • bool $disable: Default false.
  • string $object_type: The object type, usually post.
  • int $object_id: The object ID.
  • mixed $meta_value: The value being saved or removed.
  • bool $delete_all: Whether matching meta is being deleted from every object.

Example

/**
 * Deletes _myprefix_remote_data from an option instead of post meta.
 *
 * @param bool   $disable     Whether to skip the database.
 * @param string $object_type The object type.
 * @param int    $object_id   The object ID.
 * @return bool
 */
function myprefix_delete_remote_data_meta( $disable, $object_type, $object_id ) {
	delete_option( 'myprefix_remote_data_' . $object_id );
	return true;
}
add_filter( 'adsanity_disable_local_delete__myprefix_remote_data_meta', 'myprefix_delete_remote_data_meta', 10, 3 );

Page Builders

# adsanity_elementor_editor_css

Filters the stylesheets loaded into the Elementor editor for AdSanity widgets.

Parameters

  • array $urls: Stylesheet URLs.

Example

/**
 * Loads a custom stylesheet in the Elementor editor.
 *
 * @param array $urls Stylesheet URLs.
 * @return array
 */
function myprefix_elementor_editor_css( $urls ) {
	$urls[] = get_stylesheet_directory_uri() . '/css/ads-editor.css';
	return $urls;
}
add_filter( 'adsanity_elementor_editor_css', 'myprefix_elementor_editor_css' );

Advertiser Reporting Add-on

# adsanity_advertiser_reporting_email_message

Filters the expiration email sent to an advertiser, after the built-in placeholders have been replaced. It’s a good place to swap in your own placeholders.

Parameters

  • string $message: The email body.
  • int $ad_id: The ad that’s about to expire.

Example

/**
 * Replaces the *|SITE|* placeholder with the site name.
 *
 * @param string $message The email body.
 * @return string
 */
function myprefix_site_placeholder( $message ) {
	return str_replace( '*|SITE|*', get_bloginfo( 'name' ), $message );
}
add_filter( 'adsanity_advertiser_reporting_email_message', 'myprefix_site_placeholder' );

# adsanity_advertiser_reporting_validate_options

Filters the Advertiser Reporting settings right before they’re saved.

Parameters

  • array $options: The sanitized settings.
  • array $dirty: The raw submitted settings.

Example

/**
 * Never sends reminders more than 30 days out.
 *
 * @param array $options The sanitized settings.
 * @return array
 */
function myprefix_limit_reminder_timing( $options ) {
	if ( isset( $options['timing'] ) ) {
		$options['timing'] = min( 30, (int) $options['timing'] );
	}

	return $options;
}
add_filter( 'adsanity_advertiser_reporting_validate_options', 'myprefix_limit_reminder_timing' );

Conditional Ad Appearance Add-on

# adsanity_caa_default_page_templates

Filters which template files count as the “Default Template” in page template conditions.

Parameters

  • array $templates: Default page.php and index.php.

Example

/**
 * Treats singular.php as a default page template.
 *
 * @param array $templates The default template files.
 * @return array
 */
function myprefix_default_page_templates( $templates ) {
	$templates[] = 'singular.php';
	return $templates;
}
add_filter( 'adsanity_caa_default_page_templates', 'myprefix_default_page_templates' );

# adsanity_caa_autocomplete_posts_num

Filters how many posts show up in the condition search box.

Parameters

  • int $number: Default 10.
  • string $post_type: The post type being searched.

Example

/**
 * Shows more products in the condition search box.
 *
 * @param int    $number    The number of results.
 * @param string $post_type The post type being searched.
 * @return int
 */
function myprefix_condition_posts_num( $number, $post_type ) {
	return 'product' === $post_type ? 25 : $number;
}
add_filter( 'adsanity_caa_autocomplete_posts_num', 'myprefix_condition_posts_num', 10, 2 );

# adsanity_caa_autocomplete_users_num

Filters how many users show up in the condition search box.

Parameters

  • int $number: Default 10.

Example

/**
 * Shows more users in the condition search box.
 *
 * @return int
 */
function myprefix_condition_users_num() {
	return 25;
}
add_filter( 'adsanity_caa_autocomplete_users_num', 'myprefix_condition_users_num' );

# adsanity_caa_autocomplete_term_num

Filters how many terms show up in the condition search box.

Parameters

  • int $number: Default 10.

Example

/**
 * Shows more terms in the condition search box.
 *
 * @return int
 */
function myprefix_condition_terms_num() {
	return 25;
}
add_filter( 'adsanity_caa_autocomplete_term_num', 'myprefix_condition_terms_num' );

Custom Ad Sizes Add-on

# adsanity_cas_custom_css

Filters the CSS generated for your custom ad sizes. The generated CSS is cached for a day.

Parameters

  • string $css: The generated CSS.

Example

/**
 * Adds a rule to the custom ad size CSS.
 *
 * @param string $css The generated CSS.
 * @return string
 */
function myprefix_custom_size_css( $css ) {
	return $css . ' .ad-970x250 img { height: auto; }';
}
add_filter( 'adsanity_cas_custom_css', 'myprefix_custom_size_css' );

Google Analytics Tracking Add-on

# adsanity_gati_configuration_message

Filters the notice shown on the Reports screen when Google Analytics hasn’t been set up yet.

Parameters

  • string $message: The notice HTML.

Example

/**
 * Changes the notice shown before Google Analytics is set up.
 *
 * @return string
 */
function myprefix_gati_setup_message() {
	return sprintf(
		'<h2>%s</h2><p>%s</p>',
		esc_html__( 'Almost there!', 'myprefix' ),
		esc_html__( 'Ask your site admin to connect Google Analytics to see ad reports.', 'myprefix' )
	);
}
add_filter( 'adsanity_gati_configuration_message', 'myprefix_gati_setup_message' );

# adsanity_gati_reports_dashboard_filters

Filters the Google Analytics filters used to build the Reports dashboard. An event only needs to match one of them to be counted.

Parameters

  • array $filters: The GA filter expressions.
  • string $version: Always ga4. It’s only there for backward compatibility.

Example

/**
 * Also includes events sent with a "Sponsored" item category.
 *
 * @param array $filters The GA filter expressions.
 * @return array
 */
function myprefix_gati_dashboard_filters( $filters ) {
	$filters[] = 'itemCategory==Sponsored';
	return $filters;
}
add_filter( 'adsanity_gati_reports_dashboard_filters', 'myprefix_gati_dashboard_filters' );

# adsanity_gati_construct_custom_reports_filter

Filters whether a Custom Report asks Google Analytics for only the selected ads.

Parameters

  • bool $filter: Default true unless every ad is selected.

Example

add_filter( 'adsanity_gati_construct_custom_reports_filter', '__return_true' );

Rotating Ad Widget Add-on

# adsanity_raw_ids_in_group

Filters the ad IDs (and their order) in a rotation. The Weighted Ads add-on uses this to weight the rotation.

Parameters

  • array $ids: The ad IDs.
  • array $query: Deprecated. Always empty since version 1.6.5.

Example

/**
 * Always shows ad 123 first in a rotation.
 *
 * @param array $ids The ad IDs.
 * @return array
 */
function myprefix_rotation_order( $ids ) {
	if ( in_array( 123, $ids, true ) ) {
		$ids = array_merge( array( 123 ), array_diff( $ids, array( 123 ) ) );
	}

	return array_values( $ids );
}
add_filter( 'adsanity_raw_ids_in_group', 'myprefix_rotation_order' );

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.