Skip to content

Instantly share code, notes, and snippets.

Last active March 15, 2019 04:19
What would you like to do?
Advanced Ads — Convert <div> to <aside>
* Changes ad wrapper <div> element to <aside> element
* Does not work with cache-busting containers
* @param string $output ad output.
* @param object $ad Advanced_Ads_Ad object.
* @return string
add_filter( 'advanced-ads-ad-output', function( $output, $ad ) {
$type = $ad->type;
if( $type != 'group' ) {
$output = preg_replace( '/^( <div )/', '<aside ', $output );
$output = preg_replace( '/(<\/div>)($|<script>)/', "</aside>\n$2", $output );
return $output;
}, 10, 2 );
Copy link

campaignupgrade commented Mar 10, 2019

This function will convert an Advanced Ads ad wrapper from a <div> tag to an <aside> tag.

The conditional is used to avoid creating two <aside> elements in case ad group ads are used to deliver ads. It probably won't hurt anything to have nested asides, but it's cleaner not to.

We use this function to inform Google and other search engines that the ad text is not part of the post_content. This improves SEO and eliminates the chance of Google displaying the ad text in search result excerpts.

Copy link

Updated to properly convert the closing tag in case the closing tag is immediately followed by a script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment