Finding your next read
Practical craft for people building for the web.
Explore DevelopmentFinding your next read
THE FINDERPRESS TOOL DIRECTORY
Explore this tool and check the official listing for its latest features and requirements.
01 / SOURCE DETAILS
Ad Code Manager gives non-developers an interface in the WordPress admin for configuring your complex set of ad codes. Some code-level configuration may be necessary to set up Ad Code Manager. Ad tags must be added (via do_action() ) to your theme’s template files where you’d like ads to appear. Alternatively, you can incorporate ad tags into your website with our widget and shortcode. Check out the configuration guide below for the full details. A common set of parameters must also be defined for your ad provider. This includes the tag IDs used by your template, the default URL for your ad provider, and the default HTML surrounding that URL. Ad Code Manager supports Google DoubleClick For Publishers (and Async), and Google AdSense. All the logic is abstracted, however, so configuring a different provider is relatively easy. Check providers/doubleclick-for-publishers.php for an idea of how to extend ACM to suit your needs. Once this configuration is in place, the Ad Code Manager admin interface will allow you to add new ad codes, modify the parameters for your script URL, and define conditionals to determine when the ad code appears. Conditionals are core WordPress functions like is_page(), is_category(), or your own custom functions that evaluate certain expressions and then return true or false. Fork the plugin on Github and follow our development blog . Configure Ad Code Manager to manage the advertisements on your site Ad Code Manager is a VIP-sponsored plugin designed to make managing the ad codes used to display advertisements on your site easier. There’s a little bit of work you’ll need to do upfront to integrate Ad Code Manager with your theme. The high-level idea behind Ad Code Manager is that it gives non-developers an admin interface to manage 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. 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 done, 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”. 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 way of filtering: add_filter( 'acm_ad_tag_ids', 'acmx_filter_ad_tag_ids' ); function acmx_filter_ad_tag_ids( $ids ) { $ids[] = array( 'enable_ui_mapping' => true, 'tag' => '100x100_smallsquare', 'url_vars' => array( 'tag' => '100x100_smallsquare', 'height' => '100', 'width' => '100', ), ); return $ids; } Keep in mind that you’ll still need to incorporate a do_action( 'acm_tag', '100x100_smallsquare' ); in your theme 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. 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: /** * 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', ), ) ); } add_filter( 'acm_output_html','acmx_filter_output_html', 5, 2 ); /** * Register the full script output to use with each ad tag. */ function acmx_filter_output_html( $output_html, $tag_id ) { $output_html = '<!-- DFP %pos% %sz% ad tag --> <script> if ( typeof ord=='undefined' ) { ord=Math.random()*10000000000000000; } if ( typeof( dfp_tile ) == 'undefined' ) { dfp_tile=%tile% }; document.write('<script src="%url%ord=' + ord + '?"></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; } add_filter('acm_output_tokens', 'acmx_filter_output_tokens', 5, 3 ); /** * Fine tune our output tokens. * * This is the real example of how easily you can modify output * depending on your ad network specs. */ 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; } ~~~<h3>Configuration Filters</h3> There are some filters which allow you to easily customize the output of the plugin. You should place these filters in your theme's functions.php file or in another appropriate place. [Check out this gist](https://gist.github.com/1631131) to see all of the filters in action. ### `acm_ad_tag_ids` Ad tag IDs are used as a parameter when adding tags to your theme (e.g. `do_action( 'acm_tag', 'my_top_leaderboard' )`). The `url_vars` defined as part of each tag here will also be used to replace tokens in your default URL. Arguments: * array $tag_ids array of default tag IDs Example usage: Add a new ad tag called 'my_top_leaderboard' ~~~php add_filter( 'acm_ad_tag_ids', 'my_acm_ad_tag_ids' ); function my_acm_ad_tag_ids( $tag_ids ) { $tag_ids[] = array( 'tag' => 'my_top_leaderboard', // tag_id 'url_vars' => array( 'sz' => '728x90', // %sz% token 'fold' => 'atf', // %fold% token 'my_custom_token' => 'something' // %my_custom_token% will be replaced with 'something' ), ); return $tag_ids; } acm_default_url Set the default tokenized URL used when displaying your ad tags. This filter is required. Arguments: * string $url The tokenized URL of Ad Code Example usage: Set your default ad code URL add_filter( 'acm_default_url', 'my_acm_default_url' ); function my_acm_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%;"; } } acm_output_html The HTML outputted by the do_action( 'acm_tag', 'ad_tag_id' ); call in your theme. Support multiple ad formats (e.g. JavaScript or simple HTML tags) by adjusting the HTML rendered for a given ad tag. The %url% token used in this HTML will be filled in with the URL defined with acm_default_url . Arguments: * string $output_html The original output HTML * string $tag_id Ad tag currently being accessed Example usage: add_filter( 'acm_output_html', 'my_acm_output_html', 10, 2 ); function my_acm_output_html( $output_html, $tag_id ) { switch ( $tag_id ) { case 'my_leaderboard': $output_html = '<a href="%url%"><img src="%image_url%" /></a>'; break; case 'rich_media_leaderboard': $output_html = '<script> // omitted </script>'; break; default: break; } return $output_html; } acm_register_provider_slug Ad Code Manager has a built-in list of providers that it gathers by scanning the ‘providers’ directory used by the plugin. Additional providers can be added by placing the appropriate files in that directory or using the acm_register_provider_slug filter to register those that may be included as part of your theme or another plugin. When using this plugin, you are defining the provider slug as part of the e
02 / SOURCE DETAILS
The plugin requires PHP 7.4 or later. It is also tested WordPress 6.4 and later, though it may run on older versions. Since the plugin is in its early stages, there are a couple additional configuration steps: Upload ad-code-manager to the /wp-content/plugins/ directory. Activate the plugin through the ‘Plugins’ menu in WordPress. Incorporate ad tags in your theme template with do_action( 'acm_tag', 'slot' ); . Also, you can use [acm-tag id="slot"] shortcode or ACM Widget. Implement filters to make the plugin work with your provider. Configure your ad codes in the WordPress admin (Tools -> Ad Code Manager).
KEEP EXPLORING