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

Add filters for template header and footer parts

Open nakitadog opened this issue 9 years ago • 5 comments

I can see how amp_post_template_head allows me to add code to the and amp_post_template_footer allows me to add code to the footer. Is there a way to add code just after the opening tag without having to create my own template?

nakitadog avatar Sep 08 '17 23:09 nakitadog

No, there is not currently. However, the array of parts that are passed into load_parts could be filterable like for the entry header and footer. So instead of:

<?php $this->load_parts( array( 'header-bar' ) ); ?>

It could potentially be replaced with:

<?php $this->load_parts( apply_filters( 'amp_post_template_header', array( 'header-bar' ) ) ); ?>

That would then allow for you to prepend a new template before the header-bar part.

westonruter avatar Nov 11 '17 07:11 westonruter

+1

WillJW avatar Dec 21 '17 14:12 WillJW

If it helps anyone, this is how I've hacked my way around adding something above the content.

Add the custom element to the footer:

add_action('amp_post_template_footer', function () {
    echo '<div id="custom-element">Hello!</div>';
});

Add some JS to move the element where you want it:

document.querySelector('body').insertBefore(
    document.querySelector('#custom-element'),
    document.querySelector('.amp-wp-article')
);

WillJW avatar Dec 21 '17 21:12 WillJW

@WillJW I don't see how your approach will work since it depends on JS being injected into the page, and JS scripts are invalid in AMP.

westonruter avatar Jan 09 '18 04:01 westonruter

The recommended way to serve templates going forward is to opt-in to Amp theme support as introduced in 0.7 and improved in the upcoming 1.0.

westonruter avatar Jun 24 '18 07:06 westonruter