Skip to content

Instantly share code, notes, and snippets.

@campaignupgrade
Last active March 15, 2019 04:19
Show Gist options
  • Save campaignupgrade/933ac02cd2dc42711073fa91d82f3d22 to your computer and use it in GitHub Desktop.
Save campaignupgrade/933ac02cd2dc42711073fa91d82f3d22 to your computer and use it in GitHub Desktop.
Advanced Ads — Convert <div> to <aside>
<?php
/**
* 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 );
@campaignupgrade
Copy link
Author

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.

@campaignupgrade
Copy link
Author

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