Support » Plugin: AMP » Custom template for fake posts

  • Resolved genericname

    (@genericname)


    Hi,

    I’ve been going through and refactoring our code after updating the plugin from 1.4.4 to 2.0.1. I noticed that amp_prepare_render() has been deprecated.

    I was previously doing something like this:

    add_action( 'wp', function() {
    	$query_var = get_query_var( REWRITE_FLAG );
    	if ( empty( $query_var ) || !in_array( $query_var, [
                'video-single', 'another-example'
            ], true ) ) return;
    
    	global $wp_query;
    	$wp_query->queried_object = new WP_Post( new StdClass() );
    
    	amp_prepare_render();
    }, 10 );
    
    add_filter( 'amp_post_template_file', function( $template_path, $template_type, $post ) {
        $query_var = get_query_var( REWRITE_FLAG );
        if ( 'video-single' === $query_var ) {
            $template_path = __DIR__ . '/templates/video.php';
        } else if ( 'another-example' === $query_var ) {
            $template_path = __DIR__ . '/templates/example.php';
        }
    
        return $template_path;
    }, 10, 3 );

    For a bit more context video is not a real post type. We are generating single detail pages for each video(from a feed) with a rewrite rule.

    In the older version I think amp was only working when is_singular == true and it had to be a WP_Post so I faked it to bypass it. Is something like this still possible? What can I use in place of amp_prepare_render()?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Weston Ruter

    (@westonruter)

    What you are trying to do is not supported by the plugin. You can find another user sought to do something similar for using the legacy AMP post templates for non-post objects (e.g. WP_Term). You can get all the details for alternatives hacks to achieve if you really need to use the legacy post templates in this issue: https://github.com/ampproject/amp-wp/issues/5220

    Nevertheless, have you considered switching to use one of the newly available Reader themes (e.g. Twenty Twenty) rather than the AMP Legacy post templates?

    In general use of using the old AMP post templates is discouraged in favor of using a Reader theme or switching to Transitional or Standard mode. If you do this, you won’t have to go through such hacks. You can use the normal WordPress theme template system for AMP pages.

    You can read more about the Expanded Reader Mode, including how you can register your own theme to be used in Reader mode, by reading the 2.0.0 release notes: https://github.com/ampproject/amp-wp/releases/tag/2.0.0

    Thread Starter genericname

    (@genericname)

    Hi @westonruter

    After some discussion we decided to move forward with a Reader theme. Would it be possible to create a custom Reader using a child theme of the main theme?

    From what I understand, I’d have to create a plugin and then register the child theme using the amp_reader_themes filter? If it is a child theme would I then have to deregister all the extra style/scripts from the parent? I recall reading that there is a certain limit on how much css we can have.

    • This reply was modified 1 year, 10 months ago by genericname.
    Plugin Author Weston Ruter

    (@westonruter)

    @genericname yes you can! The easiest way to include your Reader theme among the list of available Reader themes is to add AMP: true to your child theme’s style.css. See for example what Neve did: https://github.com/Codeinwp/neve/pull/1959/files

    This avoids the need to create the amp_reader_themes filter. For more on that, see the 2.0.0 release notes section for Adding Reader Themes.

    Then yes, in your child theme you can dequeue the scripts which aren’t allowed in AMP. For stylesheets, the AMP plugin’s CSS tree shaker should be able to bring down the total CSS below the 75KB limit, unless your parent theme has a very large amount.

    For an example of an child theme that makes a parent theme AMP-compatible, see this Gist for the Noto Simple theme: https://gist.github.com/westonruter/b904977a296da091584a7a2264df2882

    Plugin Author albertomedina

    (@albertomedina)

    You can learn more about Reader themes here: https://amp-wp.org/documentation/getting-started/reader-mode-themes/

    Thread Starter genericname

    (@genericname)

    Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom template for fake posts’ is closed to new replies.