Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic adding of AMP component scrips #1186

Closed
scottblackburn opened this issue May 30, 2018 · 1 comment
Closed

Automatic adding of AMP component scrips #1186

scottblackburn opened this issue May 30, 2018 · 1 comment

Comments

@scottblackburn
Copy link

scottblackburn commented May 30, 2018

I was hoping for some advice regarding an issue we've had since upgrading to 0.7.1.

We use a custom template for our AMP implementation and manually add some amp-ad components into our header and footer. In addition to this, on longer posts we also add amp-ad components into the post content. Our current setup also manually adds the <script async custom-element="amp-ad" src="https://cdn.ampproject.org/v0/amp-ad-0.1.js"></script> into the head.

I understand that the plugin now automatically discovers the required AMP component scripts (#882, #885), but this only seems to work if the amp-ad components are added into post content, this detection doesn't seem to take into account any components added into the templates manually. This causes the following issues:

  1. If the template contains a amp-ad and the post content also contains an amp-ad then the amp-ad script is added twice resulting in a validation warning.
  2. If we no longer add in the amp-ad script directly to the template but also the post doesn't have any amp-ads appearing in the post content then the ad script doesn't load and the ads fail.

I understand that this issue could be caused by our implementation, but would it be possible to clarify how best to approach this situation?

@westonruter
Copy link
Member

Sure thing. Here is the best way to do it to avoid duplication across plugins:

add_filter( 'amp_post_template_data', function( $data ) {
	$data['amp_component_scripts'] = array_merge(
		$data['amp_component_scripts'],
		array(
			'amp-ad' => 'https://cdn.ampproject.org/v0/amp-ad-latest.js',
		)
	);
	return $data;
} );

Ref: https://wordpress.org/support/topic/the-tag-amp-ad-extension-js-script-appears-more-than-once-in-the-document/#post-10262578

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants