-
Notifications
You must be signed in to change notification settings - Fork 383
Customization and 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.
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.
- A 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.
The default template will attempt to draw from various theme mods, such as site icon, if supported by the active theme.
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:
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;
}If you want to hide the site text and just show a logo, use the amp_post_template_css action. The following colours the title bar black, hides the site title, and replaces it with a centered logo:
add_action( 'amp_post_template_css', 'xyz_amp_additional_css_styles' );
function xyz_amp_additional_css_styles( $amp_template ) {
// only CSS here please...
?>
nav.amp-wp-title-bar {
padding: 12px 0;
background: #000;
}
nav.amp-wp-title-bar a {
background-image: url( 'https://example.com/path/to/logo.png' );
background-repeat: no-repeat;
background-size: contain;
display: block;
height: 28px;
width: 94px;
margin: 0 auto;
text-indent: -9999px;
}
<?php
}
Note: you will need to adjust the colours and sizes based on your brand.
You can tweak various parts of the template via code.
The default template does not display the featured image currently. There are many ways to add it, such as the snippet below:
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 <img /> tag; our sanitizer will take care of it later.
$image = sprintf( '<p class="xyz-featured-image">%s</p>', get_the_post_thumbnail() );
$content = $image . $content;
}
return $content;
}By default, your theme's $content_width value will be used to determine the size of the amp content well. You can change this:
add_filter( 'amp_content_max_width', 'xyz_amp_change_content_width' );
function xyz_amp_change_content_width( $content_max_width ) {
return 1200;
}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:
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 and must be somewhere in a subfolder of WP_CONTENT_DIR.
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.
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;
}
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.
Create a folder in your theme called amp and add a file called meta-author.php with the following:
<li class="xyz-byline">
<span>Anonymous</span>
</li>Replace the contents, as needed.
This will load the file t/meta-custom-tax.php for the taxonomy section:
add_filter( 'amp_post_template_file', 'xyz_amp_set_custom_tax_meta_template', 10, 3 );
function xyz_amp_set_custom_tax_meta_template( $file, $type, $post ) {
if ( 'meta-taxonomy' === $type ) {
$file = dirname( __FILE__ ) . '/t/meta-custom-tax.php';
}
return $file;
}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:
<li class="xyz-tax-authors">
<?php echo get_the_term_list( $this->get( 'post_id' ), 'xyz-author', '', ', ' ); ?>
</li>This will completely remove the author section:
add_filter( 'amp_post_article_header_meta', 'xyz_amp_remove_author_meta' );
function xyz_amp_remove_author_meta( $meta_parts ) {
foreach ( array_keys( $meta_parts, 'meta-author', true ) as $key ) {
unset( $meta_parts[ $key ] );
}
return $meta_parts;
}This adds a new section to display the comment count:
add_filter( 'amp_post_article_footer_meta', 'xyz_amp_add_comment_count_meta' );
function xyz_amp_add_comment_count_meta( $meta_parts ) {
$meta_parts[] = 'xyz-meta-comment-count';
return $meta_parts;
}
add_filter( 'amp_post_template_file', 'xyz_amp_set_comment_count_meta_path', 10, 3 );
function xyz_amp_set_comment_count_meta_path( $file, $type, $post ) {
if ( 'xyz-meta-comment-count' === $type ) {
$file = dirname( __FILE__ ) . '/templates/xyz-meta-comment-count.php';
}
return $file;
}Then, in templates/xyz-meta-comment-count.php:
<li>
<?php printf( _n( '%d comment', '%d comments', $this->get( 'post' )->comment_count, 'xyz-text-domain' ) ); ?>
</li>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:
add_action( 'amp_post_template_css', 'xyz_amp_my_additional_css_styles' );
function xyz_amp_my_additional_css_styles( $amp_template ) {
// only CSS here please...
?>
.amp-wp-byline amp-img {
border-radius: 0; /* we don't want round avatars! */
}
.my-custom-class {
color: blue;
}
<?php
}If you'd prefer to use your own styles, you can either:
- Create a folder in your theme called
ampand add a file calledstyle.phpwith your custom CSS. - Specify a custom template using the
amp_post_template_filefilter 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.
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.
add_action( 'amp_post_template_footer', 'xyz_amp_add_pixel' );
function xyz_amp_add_pixel( $amp_template ) {
$post_id = $amp_template->get( 'post_id' );
?>
<amp-pixel src="https://example.com/hi.gif?x=RANDOM"></amp-pixel>
<?php
}If you don't want to use the default /amp endpoint, use the amp_query_var filter to change it to anything else.
add_filter( 'amp_query_var' , 'xyz_amp_change_endpoint' );
function xyz_amp_change_endpoint( $amp_endpoint ) {
return 'foo';
}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:
add_filter( 'amp_post_template_file', 'xyz_amp_set_custom_template', 10, 3 );
function xyz_amp_set_custom_template( $file, $type, $post ) {
if ( 'single' === $type ) {
$file = dirname( __FILE__ ) . '/templates/my-amp-template.php';
}
return $file;
}Note: there are some requirements for a custom template:
- You must trigger the
amp_post_template_headaction in the<head>section:
do_action( 'amp_post_template_head', $this );
- You must trigger the
amp_post_template_footeraction right before the</body>tag:
do_action( 'amp_post_template_footer', $this );
- Within your
amp-customstyletags, you must trigger theamp_post_template_cssaction:
do_action( 'amp_post_template_css', $this );- You must include all required mark-up that isn't already output via the
amp_post_template_headaction.
Notice: Please also see the plugin documentation on amp-wp.org