How is the ad markup being added to the pages in the first place?
There is no way to directly use str_replace() to replace content. It is better to use a conditional like:
<?php if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) : ?>
<amp-ad>...</amp-ad>
<?php else : ?>
...non-AMP ad code...
<?php endif; ?>
When I’m writing my entry in the wp editor, I just put xADDx in the middle of the content in the place I want the advertisement to be shown. That’s why I need to replace xADDx for nothing in AMP.
If it’s in the content then you can just use the the_content filter. I don’t know the specifics of how that replacement happens to begin with, but you could this code in your theme’s functions.php:
add_filter( 'the_content', function( $content ) {
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
$content = str_replace( 'xADDx', '', $content );
}
return $content;
}, 1 );
-
This reply was modified 7 years, 3 months ago by
Weston Ruter.
I tryed that in my child functions.php and in the wp-content/amp/includes/admin/functions.php and it didn’t work.
But I saw that in my child theme I had an “AMP” folder with footer.php and single.php.
In single.php I changed this:
<?php echo $this->get( 'post_amp_content' ); // amphtml content; no kses ?>
for this:
<?php echo str_Replace('<p>xADDx</p>', '', $this->get( 'post_amp_content' )); // amphtml content; no kses ?>
and now it doesn’t show xADDx
That isn’t the right functions.php file. It should be the one at the root of your theme.