amp-wp icon indicating copy to clipboard operation
amp-wp copied to clipboard

How do Ads get inserted?

Open kkranzo opened this issue 10 years ago • 8 comments

According to the AMP documentation, an ad would look like the below:

<amp-ad width=320 height=50
    type="doubleclick"
    data-slot="/4119129/mobile_ad_banner">
</amp-ad>

However, where/how can that get inserted using the plugin?

kkranzo avatar Feb 29 '16 15:02 kkranzo

That is describing analytics, not ads.

kkranzo avatar Feb 29 '16 15:02 kkranzo

Sorry wrong link .. https://github.com/Automattic/amp-wp/blob/master/readme.md#custom-sanitizer

portalzine avatar Feb 29 '16 16:02 portalzine

Okay, thanks!

I added the below code to my functions.php but it is not working (making the site break and appear white).

//Add advertisement to content
class XYZ_AMP_Ad_Injection_Sanitizer extends AMP_Base_Sanitizer {
    public function sanitize() {
        $body = $this->get_body_node();
        $adSlot = get_field('google_amp_ad', 'option');

        // Build our amp-ad tag
        $ad_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-ad', array(
            // Taken from example at https://github.com/ampproject/amphtml/blob/master/builtins/amp-ad.md
            'width' => 300,
            'height' => 50,
            'type' => 'doubleclick',
            'data-slot' => $adSlot,
        ) );

        // If we have a lot of paragraphs, insert before the 4th one.
        // Otherwise, add it to the end.
        $p_nodes = $body->getElementsByTagName( 'p' );
        if ( $p_nodes->length > 6 ) {
            $p_nodes->item( 4 )->insertBefore( $ad_node );
        } else {
            $body->appendChild( $ad_node );
        }
    }
}

add_filter( 'amp_content_sanitizers', 'xyz_amp_add_ad_sanitizer', 10, 2 );

function xyz_amp_add_ad_sanitizer( $sanitizer_classes, $post ) {
    require_once( dirname( __FILE__ ) . '/classes/class-ad-inject-sanitizer.php' );
    $sanitizer_classes[ 'XYZ_AMP_Ad_Injection_Sanitizer' ] = array(); // the array can be used to pass args to your sanitizer and accessed within the class via `$this->args`
    return $sanitizer_classes;
}

kkranzo avatar Feb 29 '16 19:02 kkranzo

Is that where this should be placed?

kkranzo avatar Mar 01 '16 19:03 kkranzo

You don't necessarily need to add the code via functions, you can directly add the code to a custom my-single.php and let the plugin know ''hey there is a new single.php file, use it instead of the original one which lies in the plugin folder...'' via function using the filter 'amp_post_template_file' have you customized the AMP page for your site ?? or you may read this article on customizing the AMP page and adding both Ads and Google Analytics and more to your wordpress amp page.... WordPress AMP

hope it helps...

jery61007 avatar Mar 03 '16 15:03 jery61007

That link is very helpful!

Now, the only problem is that I want to use some custom fields (Advanced Custom Fields) within my custom my-single.php file. How can I access those values?

As you can see in the original example I posted, I am trying to get a value for the ad slot that is stored in a custom field named "google_amp_ad" in my theme. Typically, this is how it would work below:

$adSlot = get_field('google_amp_ad', 'option');

However, that doesn't work. I also tried this and it didn't work either:

$adSlot = $this->get_field('google_amp_ad', 'option');

How would I go about accessing custom fields within my custom templates?

kkranzo avatar Mar 03 '16 16:03 kkranzo

You can go with this global global $post; get_post_meta( $post->ID, 'google_amp_ad' , true); but that call should be within the while loop then that will look like this

while (have_posts() ) : the_post();
global $post;

$adSlot  =     get_post_meta( $post->ID, 'google_amp_ad' , true);

endwhile;

jery61007 avatar Mar 03 '16 17:03 jery61007

//Add advertisement to content
class XYZ_AMP_Ad_Injection_Sanitizer extends AMP_Base_Sanitizer {
    public function sanitize() {
        $body = $this->get_body_node();
        $adSlot = get_field('google_amp_ad', 'option');

        // Build our amp-ad tag
        $ad_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-ad', array(
            // Taken from example at https://github.com/ampproject/amphtml/blob/master/builtins/amp-ad.md
            'width' => 300,
            'height' => 50,
            'type' => 'doubleclick',
            'data-slot' => $adSlot,
        ) );

        // If we have a lot of paragraphs, insert before the 4th one.
        // Otherwise, add it to the end.
        $p_nodes = $body->getElementsByTagName( 'p' );
        if ( $p_nodes->length > 6 ) {
            $p_nodes->item( 4 )->insertBefore( $ad_node );
        } else {
            $body->appendChild( $ad_node );
        }
    }
}

it,s work for me! How can i center the Ads? with this code there are showing only 1 Ad how to add more Ads?

Thanks!!!

jdoy1382 avatar Mar 07 '16 14:03 jdoy1382