AMP pages for Textpattern CMS.
This conditional tag examines the URL of the current page and determines if the URL ends in ‘amp.’ This allows for a custom page to be rendered using the standards for Google’s Accelerated Mobile Pages (AMP) project.
Don’t forget to check out my other plugin that’s also Google AMP compatible: pat_article_social
Using Composer:
$ composer require cara-tm/pat_if_amp:*
Or download the latest version of the plugin from the GitHub project page, paste the code into the Textpattern Plugins administration panel, install and enable the plugin.
url
string (optional): The location (URL) of the website. Default:hu
(website URL as set in the Textpattern preferences panel).subdomain
string (optional): Sets the subdomain name of where to redirect to. Default:amp
.permlink
boolean (optional): if set to0
(false) the redirection for the same article’s URL mode within the subdomain will be not made. Default:1
(true).
Place code similar to below within your individual article template header, so that the AMP page can be properly detected:
<txp:if_individual_article>
<link rel="canonical" href="<txp:permlink />">
<link rel="amphtml" href="<txp:permlink />/amp/">
</txp:if_individual_article>
Place something similar to below within your individual article template to display the alternative formatting:
<txp:pat_if_amp>
<txp:output_form form="amp_page" />
<txp:else />
<txp:output_form form="standard_page" />
</txp:mkp_if_amp>
A <txp:variable name="pat_amp" />
with a true/false value (1
or 0
) is automatically generated, for testing when this is an AMP context or not.
<txp:if_variable name="pat_amp" value="1">
AMP context
<txp:else />
Normal context
</txp:if_variable>
<txp:if_variable name="pat_amp" value="1">
...Do some AMP things...
<a href="<txp:site_url />category/<txp:category name='<txp:category1 />' />?amp">AMP Category 1 Link</a>
<txp:else />
...Do some normal (non-AMP) things...
</txp:if_variable>
A tag to render article text content without any inline styles. To use as a drop-in replacement of the standard Textpattern <txp:body />
tag and/or <txp:excerpt />
tag.
content
string (optional): choose eitherbody
orexcerpt
article content to sanitize. Default:body
.
In an article Form context:
<txp:pat_amp_sanitize content="excerpt" />
A simple tag for web redirection to a subdomain name. Same behaviour as used in The Guardian website. Must be used with a mobiles detection script (i.e. adi_mobile plugin). Some web designers could choose to use a subdomain for their mobiles websites (may be not recommended, but the official AMP Blog is located in a subdomain, see: https://amphtml.wordpress.com). In this case, the Textpattern multi-sites capacities makes things easier.
Note: Doesn’t work on local server installations, only on live servers.
<txp:pat_amp_redirect url="" subdomain="" permlink="" />
Good advice: Copy the config.php
file of the main site into the subdomain configuration folder in order to share the same database. So, all articles written by the Copy Editors will be sent automatically to the ‘AMP powered website’ from only one Textpattern administration interface attached to the ‘normal’ website. But designers have a separate and cleaner space to build their AMP pages.
url
string (optional): The location (URL) of the website. Default:hu
(website URL as set in the Textpattern preferences panel).subdomain
string (optional): Sets the subdomain name of where to redirect to. Default:amp
.permlink
boolean (optional): (v3.2 onwards) Choose to redirect the same individual article’s title URL within the subdomain. Default:0
(false).
<txp:pat_amp_redirect subdomain="amp" permlink="1" />
In this case, if your main domain is example.com
, it redirects to the amp.example.com
subdomain and uses the same individual article’s URL from the main domain.
- Version 1.0: March 24th, 2016
- Version 2.0: April 5th, 2016. Strongest support for different systems with in build GLOBAL variable. Added a Textpattern variable that sniffs for a
?amp
query in URLs. - Version 3.0: April 6th, 2016. Added a new Textpattern tag for subdomain redirection.
- Version 3.1: April 30th, 2016. Better context recognition and better variable creation. Variable name changed to
mkp_amp
. - Version 3.2: May 2nd, 2016. Support for same individual article’s URL within the subdomain.
- Version 3.3: 3rd, 2016. Support for redirection to a subdomain with the same article’s title URL when addresses have a
/amp
or?amp
query. - Version 3.5: January 13th 2017. New tag added
<txp:mkp_amp_sanitize />
that removes any inline CSS style within an article’s text contents (body/excerpt). Thanks whocarez. - May 9th 2017. Redistribution as ‘pat_if_amp’.
Many thanks to Phil Wareham who tidied up this plugin code and its help file.
Thank you to Michael K. Pate, for its original idea, who accepted to distribute my entire rewriting code as a ‘pat’ prefixed plugin.