Documentation Configure Ad Code Manager to manage the advertisements on your site

Configure Ad Code Manager to manage the advertisements on your site

Overview #

Ad Code Manager is a VIP-sponsored plugin designed to make it easier to manage the ad codes used to display advertisements on your site. There’s a little bit of work you’ll need to do up front, however, in order to integrate Ad Code Manager with your theme.

Note: WordPress.com Enterprise users should contact support and we’ll take care of the configuration for you.

The high-level idea behind Ad Code Manager is that it gives non-developers an admin interface to manage your ad codes. It then permits users to (optionally) target specific ad codes using conditionals like is_home() and is_single(). Ad codes are associated with positions in the theme through the use of ad tags.

Currently, Ad Code Manager easily integrates with Google Doubleclick For Publishers Async and Google AdSense. Other ad providers are supported with additional configuration.

↑ Top ↑

Google AdSense and DoubleClick For Publishers Async #

Let’s use AdSense as our first example. You’ll want to incorporate some of the default ad tags into your theme by use of do_action(). Here’s an example you might put in your header.php file:

do_action( 'acm_tag', '728x90_leaderboard' );

Once you’ve done so, you can select the “Google AdSense” provider in the admin. Ad codes can be registered against ad tags (positions) by choosing the ad tag from the drop down, entering the tag ID and publisher ID, and hitting “Add New Ad Code”.

Google AdSense configuration

And like that, your 728×90 leaderboard will appear on your site.

The Google AdSense configuration comes with many of Google’s suggested sizes. Additional ad tags can be registered by the way of filtering:

add_filter( 'acm_ad_tag_ids', 'acmx_filter_ad_tag_ids' );
function acmx_filter_ad_tag_ids( $ids ) {

	$ids[] = array(
		'tag'       => '100x100_smallsquare',
		'url_vars'  => array(
			'tag'       => '100x100_smallsquare',
			'height'    => '100',
			'width'     => '100',
			),
		'enable_ui_mapping' => true,
	);

	return $ids;
}

Keep in mind that you’ll still need to incorporate a do_action( 'acm_tag', '100x100_smallsquare' ); in your theme in order to display the ad tag.

If you choose Google DFP Async as your provider, you’ll likely need to register additional ad tags, as we only package two default ad tags.

↑ Top ↑

Custom Ad Provider Implementations #

As mentioned previously, other ad code providers are supported with additional configuration. Here’s an example of the different filters you would use to configure the older version of Google Doubleclick For Publishers:

<?php
/**
 * Define the default URL to be used when rendering ad codes
 */
add_filter( 'acm_default_url', 'acmx_filter_default_url' ) ;
function acmx_filter_default_url( $url ) {
	if ( 0 === strlen( $url )  ) {
		return "http://ad.doubleclick.net/adj/%site_name%/%zone1%;s1=%zone1%;s2=;pid=%permalink%;fold=%fold%;kw=;test=%test%;ltv=ad;pos=%pos%;dcopt=%dcopt%;tile=%tile%;sz=%sz%;";
	}
}

/**
 * Whitelist the DFP URL to be used in ad tags. The whitelist
 * helps prevent execution of arbitrary scripts
 */
add_filter( 'acm_whitelisted_script_urls', 'acmx_filter_whitelisted_script_urls');
function acmx_filter_whitelisted_script_urls( $whitelisted_urls ) {
	$whitelisted_urls = array( 'ad.doubleclick.net' );
	return $whitelisted_urls;
}

/**
 * Define the different ad tags (locations) you'd like to use in your theme
 */
add_filter( 'acm_ad_tag_ids', 'acmx_ad_tags_ids' );
function acmx_ad_tags_ids( $ad_tag_ids ) {
	return array(
			array(
					'tag' => '728x90-atf',
					'url_vars' => array(
						'sz' => '728x90',
						'fold' => 'atf',
						'width' => '728',
						'height' => '90',
				)
			),
			array(
					'tag' => '728x90-btf',
					'url_vars' => array(
						'sz' => '728x90',
						'fold' => 'btf',
						'width' => '728',
						'height' => '90',
				)
			) ,
			array(
					'tag' => '300x250-atf',
					'url_vars' => array(
						'sz' => '300x250',
						'fold' => 'atf',
						'width' => '300',
						'height' => '250',
				)
			),
			array(
					'tag' => '300x250-btf',
					'url_vars' => array(
						'sz' => '300x250',
						'fold' => 'btf',
						'width' => '300',
						'height' => '250',
				)
			),
			array(
					'tag' => '160x600-atf',
					'url_vars' => array(
						'sz' => '160x600',
						'fold' => 'atf',
						'width' => '160',
						'height' => '600',
				)
			),
			array(
					'tag' => '1x1',
					'url_vars' => array(
						'sz' => '1x1',
						'fold' => 'int',
						'pos' => 'top',
					)
			)
		);	
}

/**
 * Register the full <script> output to use with each ad tag
 */
add_filter( 'acm_output_html','acmx_filter_output_html', 5, 2 );
function acmx_filter_output_html( $output_html, $tag_id ) {
	$output_html = '<!-- DFP %pos% %sz% ad tag --> 
	<script language="JavaScript" type="text/javascript">
if (typeof ord==\'undefined\') {ord=Math.random()*10000000000000000;}
if (typeof(dfp_tile) == \'undefined\') dfp_tile=%tile%;
document.write(\'<script language="JavaScript" src="%url%ord=\' + ord + \'?" type="text/javascript"><\/script>\');
</script><noscript><a href="%url%ord=%random%?" target="_blank"><img src="%url%ord=%random%?" width="%width%" height="%height%" border="0" alt=""></a></noscript>
<!-- //DFP %pos% %sz% tag -->';
	return $output_html;
}

/**
 * Fine tune our output tokens
 * This is the real example of how easily you can modify output
 * depending on your ad network specs
 */
add_filter('acm_output_tokens', 'acmx_filter_output_tokens', 5, 3 );
function acmx_filter_output_tokens( $output_tokens, $tag_id, $code_to_display ) {
	global $dfp_tile;
	global $dfp_ord;
	global $dfp_pos;
	global $dfp_dcopt;
	global $wp_query;
	
	// We can't really rely on get_permalink() so use $_SERVER['REQUEST_URI] as bulletproof solution for generating unique pids
	$link = strlen( $_SERVER['REQUEST_URI'] ) > 1 ? sanitize_key( $_SERVER['REQUEST_URI'] ) : home_url();
	$output_tokens['%permalink%'] = str_replace( array( '/',':', '.' ), "", $link ); 
	$output_tokens['%random%'] = $dfp_ord;
	$output_tokens['%tile%'] = ++$dfp_tile;
	if (  false === $dfp_pos[ $code_to_display['url_vars']['sz'] ] ) {
		$output_tokens['%pos%'] = 'top';
		$dfp_pos[ $code_to_display['url_vars']['sz'] ] = true;
	} else {
		$output_tokens['%pos%'] = 'bottom';
	}
	if ( ! $dfp_dcopt ) {
		$output_tokens['%dcopt%'] = 'ist';
		$dfp_dcopt = true;
	} else {
		$output_tokens['%dcopt%'] = '';
	}
	
	$output_tokens['%test%'] = isset( $_GET['test'] ) && $_GET['test'] == 'on' ? 'on' : '';
	
	return $output_tokens;
}