-
Notifications
You must be signed in to change notification settings - Fork 382
Description
The plugin in amp theme support currently supports two modes:
- Canonical: Everything is canonical AMP.
- Paired: Everything is available in non-AMP and some content is also available in AMP, with different templates used in AMP mode.
There is a third case that should be supported and that is:
- Mixed-Some-Optionally-AMP: All content is available in non-AMP, and some content is also available in AMP, but they use the same theme templates.
Now that we have full-document sanitization in #929, the a non-AMP template can actually be served as totally valid AMP, with amp attribute added to the html element, meta viewport being added, and CSS boilerplate code. So we can actually use the exact same templates in AMP as non-AMP. We don't readily make this easy in paired mode, as to do paired mode the amp theme support must define a template_dir for where to locate the AMP templates, like:
add_theme_support( 'amp', array(
'template_dir' => 'amp-templates/'
) );When a paired-mode site only allows a subset of content to be shown in AMP (e.g. just singular posts), they can explicitly indicate this with the available_callback property:
add_theme_support( 'amp', array(
'template_dir' => 'amp-templates/'
'available_callback' => 'is_single',
) );However, in order to support re-using templates between AMP and non-AMP we should also be able to do:
add_theme_support( 'amp', array(
'available_callback' => 'is_single',
) );Without this, you have to do a needless hack of setting the template_dir to . or add a single.php in the template directory that just does require __DIR__ . '../single.php';
Lastly, there is a fourth mode that should be doable:
- Mixed-Some-Only-AMP: Some content is not available in AMP, and everything else is only available in AMP.
In the fourth mode there should not be two versions of any one URL. There would be no /amp/ endpoints, and no amphtml links on AMP content. Content in AMP and non-AMP would be disjoint sets. To do this, there would need to probably need to be something different from available_callback in the theme support, or rather instead of this callback returning a boolean flag it should actually be tri-state:
- AMP Omitted
- AMP Optional
- AMP Only
For example, if you wanted to serve AMP-only posts, AMP optionally for pages, and omit AMP everywhere else, you could do:
add_theme_support( 'amp', array(
'available_callback' => function() {
if ( is_single() ) {
return AMP_ONLY; // No amphtml version offered. AMP 100%.
} elseif ( is_page()) {
return AMP_OPTIONAL; // Non-AMP by default. Link to amphtml URL offered to opt-in.
} else {
return AMP_OMITTED; // Non-AMP always. No link to amphtml URL offered at all.
}
}
) );