Adding your own placement

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.

You can add your own placement to Advanced Ads. The example below does that for a specific action hook. You can adjust it according to your own purpose.

Just change the ADVADS_CUSTOM_HOOK_PLACEMENT_HOOK_NAME to any action hook you want to show the ads in.

The whole code from below can go into a new PHP file which you can then move to the wp-content/plugins folder to work as a custom plugin.

When enabled, the plugin adds a new placement type to which you can assign ads or set options like labels or Cache Busting.

<?php
/**
 * Advanced Ads – Custom Hook Placement
 *
 * @wordpress-plugin
 * Plugin Name:       Advanced Ads – Custom Hook Placement
 * Plugin URI:        https://wpadvancedads.com
 * Description:       Manage and optimize your ads in WordPress
 * Version:           0.1
 * Author:            Thomas Maier
 * Author URI:        https://wpadvancedads.com
 * Domain Path:       /languages
 * License:           GPL-2.0+
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.txt
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * change the action hook here
 */
define( 'ADVADS_CUSTOM_HOOK_PLACEMENT_HOOK_NAME', 'ampforwp_before_post_content' );

add_action( 'plugins_loaded', 'advanced_ads_custom_hook_placement_plugins_loaded_ad_actions' );

/**
 * Initialize this add-on
 */
function advanced_ads_custom_hook_placement_plugins_loaded_ad_actions() {
	// Stop, if main plugin doesn’t exist.
	if ( ! class_exists( 'Advanced_Ads', false ) ) {
		return;
	}
	// Load the dynamic hook.
	$placements = get_option( 'advads-ads-placements', array() );

	foreach ( $placements as $_placement_id => $_placement ) {
		if ( isset( $_placement['type'] ) && 'custom_hook_ampforwp' == $_placement['type'] ) {
			add_action( ADVADS_CUSTOM_HOOK_PLACEMENT_HOOK_NAME, 'advanced_ads_custom_hook_placement_execute_hook' );
		}
	}
}

function advanced_ads_custom_hook_placement_execute_hook() {

	// Get placements.
	$placements = get_option( 'advads-ads-placements', array() );

	// Look for the current hook in the placements.
	$hook = current_filter();
	foreach ( $placements as $_placement_id => $_placement ) {
		if ( isset( $_placement['type'] ) && 'custom_hook_ampforwp' == $_placement['type']
			&& $hook === ADVADS_CUSTOM_HOOK_PLACEMENT_HOOK_NAME ) {
			// the_ad_placement( $_placement_id );
			$_options = isset( $_placement['options'] ) ? $_placement['options'] : array();
			echo Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options );
		}
	}

}

/**
 * Backend functions
 */
add_action( 'plugins_loaded', 'advanced_ads_custom_hook_placement_admin_plugins_loaded' );
function advanced_ads_custom_hook_placement_admin_plugins_loaded() {
	if ( ! class_exists( 'Advanced_Ads_Admin', false ) ) {
		return;
	}

	// Add custom hook placement.
	add_action( 'advanced-ads-placement-types', 'advanced_ads_custom_hook_placement_add_placement' );

}

/**
 * Add placement
 */
function advanced_ads_custom_hook_placement_add_placement( $types ) {

	// Placement title and description
	$types['custom_hook_ampforwp'] = array(
		'title'       => 'AMP for WP Hook',
		'description' => 'Custom Hook Placement'
	);

	return $types;
}

Make it better

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