The AMP (Accelerated Mobile Pages Project) plugin for WordPress is still pretty young and under heavy development.

There are little things that can help to fix current validation problems. I have a couple of fresh AMP pages, that are already indexed by Google.

0. RESOURCES


1. MISSING IMAGE (schema.org-NewsArticle)

The currently registered Microformats template (schema.org-NewsArticle) requires an image.
The AMP-WP documentation explains how to add a featured image to your pages (documentation).

I extended that a bit,  to link a default attachment image, I uploaded before. This needs to be added to your themes functions.php.

  1. function xyz_amp_add_custom_actions() {
  2.     add_filter( 'the_content', 'xyz_amp_add_featured_image' );
  3. }
  4.  
  5. function xyz_amp_add_featured_image( $content ) {
  6.  
  7.  $your_attachment_id = 2016;
  8.  
  9.  if ( has_post_thumbnail() ) {
  10.   $image = sprintf( '<p class="xyz-featured-image">%s</p>', get_the_post_thumbnail() );
  11.        
  12.  }else{
  13.  $image = sprintf( '<p class="xyz-featured-image">%s</p>', wp_get_attachment_image( $your_attachment_id, 1280) );
  14.         $content = $image . $content ;
  15.  }
  16.  $content = $image . $content ;
  17.  return preg_replace('/(<br>\s*){2,}/', '<br/>',$content);
  18. }

Next we adjust the Microformats definition. This needs to be added to your themes functions.php.

  1. add_filter( 'amp_post_template_metadata', 'xyz_amp_modify_json_metadata', 10, 2 );
  2.  
  3. function xyz_amp_modify_json_metadata( $metadata, $post ) {
  4.   if(empty($metadata['image'])){
  5.     $your_attachment_id = 2016;
  6.     $imgMeta = wp_get_attachment_metadata($your_attachment_id );
  7.     $metadata['image'] = array(
  8.         '@type' => 'ImageObject',
  9.         'url' => wp_get_attachment_url( $your_attachment_id ),
  10.         'height' => $imgMeta['height'],
  11.         'width' =>  $imgMeta['width'],
  12.     );
  13.   }
  14.     return $metadata;
  15. }

2. ADD YOUR OWN AMP TEMPLATE

Read the AMP-WP documentation first :) This forces AMP-WP to override the core template with your own. Use the default template as a reference.

  1. add_filter( 'amp_post_template_file', 'xyz_amp_set_custom_template', 10, 3 );
  2.  
  3. function xyz_amp_set_custom_template( $file, $type, $post ) {
  4.     if ( 'single' === $type ) {
  5.         $file = dirname( __FILE__ ) . '/amp/amp_template.php';
  6.     }
  7.     return $file;
  8. }

3. REMOVE NONE VALIDATING CONTENT

Normally the core sanitizer should take care of that.  I am sure a filter will be added in the future. For now, this is a quick workaround. You can use a RegExp or DOMDocument to alter the amp-content in your custom template and filter things that break validation.

  1. $attributes = $element->attributes;
  2. while ($attributes->length) {
  3.     $element->removeAttribute($attributes->item(0)->name);
  4. }

I am using phpQuery for now, as it allows me to add quick fixes. DOMNode or QueryPath works nicely as well :) Will be looking at the core sanitizer and options later this week. All of this is currently added to the custom template.

  1.  require('your-libs/phpQuery/phpQuery.php');
  2.  $doc = phpQuery::newDocumentHTML($this->get( 'post_amp_content' ));
  1. Remove REL attributes that are not allowed. Only registered Microformat rel attributes are allowed.
    1.  $doc['.prettyphoto']->removeAttr("rel");
  2. Remove attribute ALIGN
    1. $doc['*']->removeAttr("align");
  3. Fix TARGET Attribute
    1.  $doc['a[target="new"]']->attr("target","_blank");

Finally output the fixed content as HTML.

  1.  echo  $doc->html(); // amphtml content; no kses

The above examples need to be tweaked to your specific content requirements.

The custom template “post_amp_content” before:

  1.  echo $this->get( 'post_amp_content'); // amphtml content; no kses

The custom template “post_amp_content” after:

  1.  require('your-libs/phpQuery/phpQuery.php');
  2.  $doc = phpQuery::newDocumentHTML($this->get( 'post_amp_content' ));
  3.  $doc['.prettyphoto']->removeAttr("rel")->removeAttr("href");
  4.  $doc['*']->removeAttr("align");
  5.  $doc['a[target="new"]']->attr("target","_blank");
  6.  echo  $doc->html(); // amphtml content; no kses

4. ADD PREVIOUS & NEXT POST NAVIGATION

From the AMP specification :”Note, that AMP document may also be linked to directly…”

This is being added to the custom template as well.

Next:

  1. global $query_string;
  2. $posts = query_posts($query_string); if (have_posts()) : while (have_posts()) : the_post();
  3.  
  4. $next_post = get_next_post();
  5. if (!empty( $next_post )):
  6.  echo '<a class="next" href="'.amp_get_permalink( $next_post->ID ).'"> &#8604; '.$next_post->post_title.'</a>';
  7. endif;
  8.  

Previous:

  1. $prev_post = get_previous_post();
  2.  
  3. if (!empty( $prev_post )):
  4.  echo '<a class="prev" href="'.amp_get_permalink( $prev_post->ID ).'"> &#8604; '.$prev_post->post_title.'</a>';
  5. endif;

5. SYNTAX HIGHLIGHTING

You can find a simple pure PHP highlighter here. Not perfect, but a start. Don not forget to include the CSS :)

  1. require_once("your-lib/SyntaxHighlight/SyntaxHighlight.php");
  2. foreach(pq('pre') as $code)
  3. {
  4.  pq($code)->html(SyntaxHighlight::process(pq($code)->text()));
  5. }

 

I will extend on this as new validation errors or tweaks come up. The core sanitizer of AMP-WP still needs some work and should allow us to do some of the above directly, when the content is parsed / converted for AMP.


 

6. OTHER AMP ARTICLES

 

Enjoy coding …

 

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

4 weeks ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago

Archaeology Travel Booth – Travel Innovation Summit 2023

Archaeology Travel is an online travel guide for people who enjoy exploring the world’s pasts.… Read More

6 months ago