Roots Discourse

Sage 9 + AMP

sage9

#1

Hi,

I’m trying to setup an AMP installation for my current theme using the Google AMP Plugin.

I’ve configured AMP so it’s running in the Paired mode. Sage 9 (my theme) dosen’t seems to find the amp folder that holds my overrides for the template files. The AMP folder is in the resources folder but it dosen’t work. I’ve tried to use the “add_theme_support(‘AMP’)” function and specify the template_dir path with no luck … Somebody that knows how to do?

Cheeers.


#2

Hey @nahoj - if you’re still having this problem, I can take a look and see if I can help. What plugin are you using for AMP?


#3

Thanks, and yes still having problem to get this working. I’m using the official AMP for Wordpress plugin. (https://sv.wordpress.org/plugins/amp/)


#4

Hey @nahoj - from looking at the plugin docs and source a little bit and experimenting, it seems like when you choose “Paired” mode it just uses the theme’s normal templates, not AMP-specific templates, and you would have to use “Classic” mode for it to use the plugin’s template files (and allow you to override those template files in theme).

When I activated the plugin and chose “Paired” mode, http://mysite.com/post-name/?amp variants of my post URLs became available that served the AMP formatted version of the posts. For example, looking at the source you can see the AMP tags have been added and the site’s normal scripts have been removed.

Once I switched to “Classic” mode, it displayed using the plugin’s templates, and I was also able to override the files. By default, you’ll need to put them in resources/amp/, but you could filter that if you wanted to put them in views somewhere.


#5

S’up! Hm, if you look here https://pre-prod.amp-wp.org/documentation/how-the-plugin-works/amp-plugin-serving-strategies/ under the section for “Paired Mode” then you can see that you can override templates based on the “template_dir” path… It should work with both… Maybe you have the same problem as me now? Paired mode dosen’t work correctly? It’s like when ur in Paired Mode AMP dosen’t find the AMP folder or just ignore it… Nothing happens.

My goal is to have “?/amp” in my url with overrides for my templates and therefor i’ve most use paired mode, right?


#6

Oh, you’re right - I missed that! Thanks for the pointer.

Looks like the AMP plugin prepends the template_dir you provide to the normal paths checked in the template hierarchy.

With this setting:

add_theme_support('amp', ['template_dir' => 'amp']);

Below is what’s checked, from most to least specific, for a single post. <post-slug> is the slug of the post being displayed.

amp/views/single-post-<post-slug>.blade.php
views/single-post-<post-slug>.blade.php
amp/views/single-post-<post-slug>.php
views/single-post-<post-slug>.php
amp/resources/views/single-post-<post-slug>.blade.php
resources/views/single-post-<post-slug>.blade.php
amp/resources/views/single-post-<post-slug>.php
resources/views/single-post-<post-slug>.php
amp/single-post-<post-slug>.blade.php
single-post-<post-slug>.blade.php
amp/single-post-<post-slug>.php
single-post-<post-slug>.php
amp/views/single-post.blade.php
views/single-post.blade.php
amp/views/single-post.php
views/single-post.php
amp/resources/views/single-post.blade.php
resources/views/single-post.blade.php
amp/resources/views/single-post.php
resources/views/single-post.php
amp/single-post.blade.php
single-post.blade.php
amp/single-post.php
single-post.php
amp/views/single.blade.php
views/single.blade.php
amp/views/single.php
views/single.php
amp/resources/views/single.blade.php
resources/views/single.blade.php
amp/resources/views/single.php
resources/views/single.php
amp/single.blade.php
single.blade.php
amp/single.php
single.php
amp/views/singular.blade.php
views/singular.blade.php
amp/views/singular.php
views/singular.php
amp/resources/views/singular.blade.php
resources/views/singular.blade.php
amp/resources/views/singular.php
resources/views/singular.php
amp/singular.blade.php
singular.blade.php
amp/singular.php
singular.php
amp/views/index.blade.php
views/index.blade.php
amp/views/index.php
views/index.php
amp/resources/views/index.blade.php
resources/views/index.blade.php
amp/resources/views/index.php
resources/views/index.php
amp/index.blade.php
index.blade.php
amp/index.php
index.php

Given that knowledge, I would put your custom AMP templates in resources/amp/views/, essentially creating parallel template hierarchies for AMP and non-AMP content in resources/amp/views/ and resources/views/, respectively.

Another approach you might like better is to omit the custom template_dir option and just do this:

add_filter('template_include', function ($template) {
    if (is_amp_endpoint()) {
        $amp_template = locate_template(['amp/'.basename($template)]);

        return ($amp_template) ? $amp_template : $template;
    }

    return $template;
}, 100);

Then you put your AMP templates in resources/views/amp/.

Depending on how extensively you need to customize your AMP templates, you may find using their filters or using the is_amp_endpoint() conditional in your normal theme templates a shorter route.


#7

Okey, so if I move my AMP Templates to views folder, it should work?


#8

Either put them in resources/amp/views/ without adding the filter, or add the filter and put them in resources/views/amp.


#9

Sweet! Can I’ve use .blade.php or is it just .php?


#10

I think I tested it and Blade worked, but don’t remember 100% for sure at this point.


#11

Blade seems to work just fine! Thank you so much for your helping hand, really appreciate it. Thanks man :slight_smile:


closed #12

This topic was automatically closed after 42 days. New replies are no longer allowed.