Change div container to another HTML element

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.

Some users might want to change the div container Advanced Ads uses for ad placeholders in Advanced Ads Pro Cache Busting or when some output options like a margin are selected.

The code below changes the div into an article container.

Please notice that this could break some other parts of Advanced Ads, especially JavaScript-based ad placements. Changing the container element is not officially supported.

/**
 * Change <div> container for ads to <article>
 * Does not work with cache-busting containers
 *
 * @param string $output ad output.
 * @param object $ad Advanced_Ads_Ad object.
 *
 * @return string
 */
function advads_change_wrapper_div( $output, $ad ) {
	$output = preg_replace( '/^(<div )/', '<article ', $output );
	$output = preg_replace( '/(<\/div>)$/', '</article>', $output );

	return $output;
}
add_filter( 'advanced-ads-ad-output', 'advads_change_wrapper_div', 10, 2 );

/**
 * Change <div> container for cache-busting ads to <article>
 *
 * @param string $output ad output.
 * @param object $ad Advanced_Ads_Ad object.
 * @param array  $args additional arguments.
 *
 * @return string
 */
function advads_change_passive_wrapper_div( $output, $ad, $args ) {
	$output = preg_replace( '/^(<div )/', '<article ', $output );
	$output = preg_replace( '/(<\/div>)$/', '</article>', $output );

	return $output;
}
add_filter( 'advanced-ads-ad-select-override-by-ad', 'advads_change_passive_wrapper_div', 20, 3 );

A user of Advanced Ads shares his implementation of the above code limiting it by single ads (instead of ads and groups).

See this gist.

Make it better

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