pat_if_amp
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.
Links
Don’t forget to check out my other plugin that’s also Google AMP compatible: pat_article_social
Note
Since TXP 4.7+ and its new features, you can choose to use build-in tags solution instead of this plugin. Read more about from this forum post by Oleg (don’t forget to add a TXP variable on the fly for further usages within you website):
<txp:evaluate query='contains(<txp:page_url escape="quote" />, "/amp")'>
AMP page
<txp:else />
regular page
</txp:evaluate>
Installing
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.
pat_if_amp
Attributes
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).
Example
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>
Textpattern variable usage
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.
Usage
<txp:if_variable name="pat_amp" value="1">
AMP context
<txp:else />
Normal context
</txp:if_variable>
Example
<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>
This example concerns categories, use kind of the same model for pages
and/or archives
.
pat_amp_sanitize
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.
Attributes
content
string (optional): choose eitherbody
orexcerpt
article content to sanitize. Default:body
.
Usage
In an article Form context:
<txp:pat_amp_sanitize content="excerpt" />
pat_amp_redirect
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.
Usage
<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.
Attributes
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): Choose to redirect the same individual article’s title URL within the subdomain. Default:0
(false).
Example
<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.
Acknowledgments
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.