😂Handling AMP Legacy Themes Full (ok)
https://amp-wp.org/documentation/playbooks/classic-templates/
Customizer Panel
The plugin adds an “AMP” panel in the Customizer, which you can use to tweak various parts of the default template like colors.
Disabling the AMP Customizer Settings
If you’re using a completely custom template, you may want to disable the AMP Customizer Settings:
Note that this needs to be called before the after_setup_theme
hook to work.
Where to Put Your Code
The code snippets below and any other code-level customizations should happen in one of the following locations.
If you’re using an off-the-shelf theme (like from the WordPress.org Theme Directory):
A child theme.
A custom plugin that you activate via the Dashboard.
An mu-plugin.
If you’re using a custom theme:
functions.php
(or via a ‘require’ call to files that load fromfunctions.php
).Any of the options above.
Theme Mods
The default template will attempt to draw from various theme mods, such as site icon, if supported by the active theme.
Site Icon
If you add a site icon, we will automatically replace the WordPress logo in the template. Or if you’d prefer you can do it programmatically:
Logo Only
If you want to hide the site text and just show a logo, use the amp_post_template_css
action. The following colors the title bar black, hides the site title, and replaces it with a centered logo:
Note: you will need to adjust the colors and sizes based on your brand.
Template Tweaks
Featured Image
The default template displays the featured image. If you don’t want to display the featured image in your amp page, use the following code:
Content Width
By default, your theme’s $content_width
value will be used to determine the size of the amp
content well. You can change this:
Template Data
Use the amp_post_template_data
filter to override default template data. The following changes the placeholder image used for iframes to a file located in the current theme:
Note: The path must pass the default criteria set out by validate_file
and must be somewhere in a subfolder of WP_CONTENT_DIR
.
Schema.org (JSON) Metadata
The plugin adds some default metadata to enable “Rich Snippet” support. You can modify this using the amp_post_template_metadata
filter. The following changes the type annotation to NewsArticle
(from the default BlogPosting
) and overrides the default Publisher Logo.
Template Meta (Author, Date, etc.)
For the meta section of the template (i.e. author, date, taxonomies, etc.), you can override templates for the existing sections, remove them, add new ones.
Example: Override Author Template from Theme
Create a folder in your theme called amp
and add a file called meta-author.php
with the following:
Replace the contents, as needed.
Example: Adding custom templates for homepage and page for posts
As for v0.6 pages are supported by the plugin, including the homepage (page on front) and page for posts (blog). After enabling “page” post type support in the AMP general settings, all pages will be enabled for AMP by default, unless the page is the homepage, the blog page, or a page that is assigned to a custom template. (You can auto opt-in to AMP support for all pages via add_filter( 'amp_post_status_default_enabled', '__return_true' )
.) To opt-in these pages for AMP, just enable support via the AMP support toggle in the page’s publish metabox. The homepage can re-use the single.php
template as a default, but the page for posts should get a custom template assigned so that the list of posts (The Loop) is present. To modify the template for the homepage or page for posts, put the following in your theme’s functions.php
:
Then you can add an amp/home.php
which is basically a fork of the plugin’s bundled single.php
and the same for amp/blog.php
with this key addition:
Example: Override Taxonomy Template via filter
This will load the file t/meta-custom-tax.php
for the taxonomy
section:
In t/meta-custom-tax.php
, you can add something like the following to replace the default category and tags with your custom author
taxonomy:
Example: Remove Author from header_meta
This will completely remove the author section:
Example: Add Comment Count to footer_meta
This adds a new section to display the comment count:
Then, in templates/xyz-meta-comment-count.php
:
Custom CSS
Rule Additions
If you want to append to the existing CSS rules (e.g. styles for a custom embed handler), you can use the amp_post_template_css
action:
Completely Override CSS
If you’d prefer to use your own styles, you can either:
Create a folder in your theme called
amp
and add a file calledstyle.php
with your custom CSS.Specify a custom template using the
amp_post_template_file
filter for'style' === $type
. See the “Override” examples in the “Meta” section for examples.
Note: the file should only include CSS, not the <style>
opening and closing tag.
Head and Footer
If you want to add stuff to the head or footer of the default AMP template, use the amp_post_template_head
and amp_post_template_footer
actions.
AMP Endpoint
If you don’t want to use the default /amp
endpoint, use the amp_query_var
filter to change it to anything else.
Custom Template
If you want complete control over the look and feel of your AMP content, you can override the default template using the amp_post_template_file
filter and pass it the path to a custom template:
Note: there are some requirements for a custom template:
You must trigger the amp_post_template_head
action in the <head>
section:
You must trigger the amp_post_template_footer
action right before the </body>
tag:
Within your amp-custom
style
tags, you must trigger the amp_post_template_css
action:
You must include all required mark-up that isn’t already output via the amp_post_template_head
action.
Custom Post Type Support
By default, the plugin only creates AMP content for posts and (as of 0.6) pages. Also as of 0.6, you can enable AMP support for other post types via the AMP settings admin screen. Just enable the checkbox with each post type which is able to be served as AMP.
You can also add support for other post types by adding add_post_type_support()
calls in PHP for the desired post types. For example, assuming our post type is xyz-review
you can add AMP support by adding the following to a theme or plugin:
You’ll need to flush your rewrite rules after this. You can do this by accessing the Permalinks admin screen or via running wp rewrite flush
in WP-CLI.
If you want to add post type support but have AMP be disabled by default, you can use the following plugin code:
If you want a custom template for your post type:
We may provide better ways to handle this in the future.
Last updated