Add filters for template header and footer parts
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?
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.
+1
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 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.
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.