Skip to content

⚡ A Textpattern Conditional Plugin for Google's Accelerated Mobile Pages Project (AMP)

License

Notifications You must be signed in to change notification settings

cara-tm/pat_if_amp

Repository files navigation

pat_if_amp

AMP pages for Textpattern CMS (http://textpattern.io/).

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

pat_if_amp

Attributes (v3.3 onwards)

  • 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 to 0 (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>

Textattern 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>

pat_amp_sanitize (v3.5 onwards)

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 either body or excerpt article content to sanitize. Default: body.

Usage

In an article Form context:

<txp:pat_amp_sanitize content="excerpt" />

pat_amp_redirect (v3.0 onwards)

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): (v3.2 onwards) 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.

Release history

  • 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’.

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.

About

⚡ A Textpattern Conditional Plugin for Google's Accelerated Mobile Pages Project (AMP)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%