# AMP for WordPress
## Overview
This plugin adds support for the [Accelerated Mobile Pages](https://www.ampproject.org) (AMP) Project, which is an an open source initiative that aims to provide mobile optimized content that can load instantly everywhere.
With the plugin active, all posts on your site will have dynamically generated AMP-compatible versions, accessible by appending `/amp/` to the end your post URLs. For example, if your post URL is `http://example.com/2016/01/01/amp-on/`, you can access the AMP version at `http://example.com/2016/01/01/amp-on/amp/`. If you do not have [pretty permalinks](https://codex.wordpress.org/Using_Permalinks#mod_rewrite:_.22Pretty_Permalinks.22) enabled, you can do the same thing by appending `?amp=1`, i.e. `http://example.com/2016/01/01/amp-on/?amp=1`
Note #1: that Pages and archives are not currently supported.
Note #2: this plugin only creates AMP content but does not automatically display it to your users when they visit from a mobile device. That is handled by AMP consumers such as Google Search. For more details, see the [AMP Project FAQ](https://www.ampproject.org/docs/support/faqs.html).
## Customization / Templating
The plugin ships with a default template that looks nice and clean and we tried to find a good balance between ease and extensibility when it comes to customization.
You can tweak small pieces of the template or the entire thing depending on your needs.
### Where Do I Put My 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](https://developer.wordpress.org/themes/advanced-topics/child-themes/).
- A custom plugin that you activate via the Dashboard.
- A [mu-plugin](https://codex.wordpress.org/Must_Use_Plugins).
If you're using a custom theme:
- `functions.php` (or a file `require`-ed by `functions.php`).
- Any of the options above.
### Theme Mods
The default template will attempt to draw from various theme mods, such as site icon and background and header color/image, if supported by the active theme.
#### Site Icon
If you add a site icon, we will automatically replace the WordPress logo in the template.
If you'd prefer to do it via code:
```php
add_filter( 'amp_post_template_data', 'xyz_amp_set_site_icon_url' );
function xyz_amp_set_site_icon_url( $data ) {
// Ideally a 32x32 image
$data[ 'site_icon_url' ] = get_stylesheet_directory_uri() . '/images/amp-site-icon.png';
return $data;
}
```
#### Custom Header
This needs to be implemented.
### Template Tweaks
You can tweak various parts of the template via code.
#### Featured Image
The default template does not display the featured image currently. There are many ways to add it, such as the snippet below:
```php
add_action( 'pre_amp_render_post', 'xyz_amp_add_custom_actions' );
function xyz_amp_add_custom_actions() {
add_filter( 'the_content', 'xyz_amp_add_featured_image' );
}
function xyz_amp_add_featured_image( $content ) {
if ( has_post_thumbnail() ) {
// Just add the raw tag; our sanitizer will take care of it later.
$image = sprintf( '
%s
', get_the_post_thumbnail() ); $content = $image . $content; } return $content; } ``` #### 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: ```php add_filter( 'amp_content_max_width', 'xyz_amp_change_content_width' ); function xyz_amp_change_content_width( $content_max_width ) { return 1200; } ``` #### 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: ```php add_filter( 'amp_post_template_data', 'xyz_amp_set_custom_placeholder_image' ); function xyz_set_custom_placeholder_image( $data ) { $data[ 'placeholder_image_url' ] = get_stylesheet_directory_uri() . '/images/amp-iframe-placeholder.png'; return $data; } ``` Note: The path must pass the default criteria set out by [`validate_file`](https://developer.wordpress.org/reference/functions/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](https://developers.google.com/structured-data/rich-snippets/articles). 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. ``` add_filter( 'amp_post_template_metadata', 'xyz_amp_modify_json_metadata', 10, 2 ); function xyz_amp_modify_json_metadata( $metadata, $post ) { $metadata['@type'] = 'NewsArticle'; $metadata['publisher']['logo'] = array( '@type' => 'ImageObject', 'url' => get_template_directory_uri() . '/images/my-amp-metadata-logo.png', 'height' => 60, 'width' => 600, ); return $metadata; } ``` #### 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: ```php