• Resolved reasonx

    (@reasonx)


    Hi,

    since I am developing on my local machine, I have not generated a UUID, because most of the details are different for the live machine. But I think these are important:

    System

    • WordPress version 5.9.2
    • AMP version 2.2.4
    • AMP plugin configured: Yes
    • AMP all templates supported: No
    • AMP supported templates: is_singular
    • AMP supported post types: post, magazin, kaufberatung, roaming, test, testbericht, stoerung, deal

    Problem

    The AMP Plugin is working fine with most of our custom post types we have set up. We are using our own amp theme in reader mode which also works fine. So no problems until then. But there is one thing we can′t get to work. After spending several days trying to find a solution we have now decided to ask you here.

    We are using a custom routing for one of our custom post types (testbericht). Without the routing the url would be /testberichte/post-name. But with our routing it is /handys/xxxxxx/test. The AMP Endpoint we are using is /amp. So that has to be added to the url for viewing the AMP version.

    Now if we go to /handys/xxxxxx/test/amp we are instantly redirected to the non-amp version without the endpoint. We have already found out where in the code that happens.
    The method redirect_extraneous_paired_endpoint() calls amp_is_available() which calls AMP_Theme_Support::get_template_availability( $wp_query ) which calls $query->is_singular() at the end which returns false. So if we set $query->is_singular = true; and $query->is_single = true; right before the check, the redirect is not happening anymore. But guess what: the amp page still does not work because if is_singular is not set there have to be some general issues with the query because the post is a singular post.

    As we don′t want to code in the Plugin itself anyway we removed the lines and set up a function for the hook “pre_get_posts” in which we tried out stuff like that:

    $query->is_singular = true;
     $query->is_single = true;
     $query->is_home = false;
    
     $query->set('singular', true);
     $query->set('is_singular', true);
     $query->set('post_type', 'testbericht');
     $query->set('p', xxxxxx);

    but it doesn′t help. At the point the amp plugin is checking for $query->is_singular() the property is not set although we’ve set it before at pre_get_posts as mentioned.

    Maybe the plugin resets these properties when specific checks are performed. We do not have a clue anymore. Maybe there is one property we have to set and everything works ?? Maybe the plugin is not able to work with custom routings.

    So if someone has an idea how to solve this, we would be very grateful ?? Thank you very much.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Milind More

    (@milindmore22)

    Hello @reasonx

    Thank you for contacting us, I am not sure about the context custom post type (how you created it and if there are any custom templates ) but when I created a custom post type and tested it on my test site with just Twenty Twenty one, the AMP plugin active and it seems to be working without issues.

    /**
     * Register a custom post type called "book".
     *
     * @see get_post_type_labels() for label keys.
     */
    function wpdocs_codex_book_init() {
        $labels = array(
            'name'                  => _x( 'Books', 'Post type general name', 'textdomain' ),
            'singular_name'         => _x( 'Book', 'Post type singular name', 'textdomain' ),
            'menu_name'             => _x( 'Books', 'Admin Menu text', 'textdomain' ),
            'name_admin_bar'        => _x( 'Book', 'Add New on Toolbar', 'textdomain' ),
            'add_new'               => __( 'Add New', 'textdomain' ),
            'add_new_item'          => __( 'Add New Book', 'textdomain' ),
            'new_item'              => __( 'New Book', 'textdomain' ),
            'edit_item'             => __( 'Edit Book', 'textdomain' ),
            'view_item'             => __( 'View Book', 'textdomain' ),
            'all_items'             => __( 'All Books', 'textdomain' ),
            'search_items'          => __( 'Search Books', 'textdomain' ),
            'parent_item_colon'     => __( 'Parent Books:', 'textdomain' ),
            'not_found'             => __( 'No books found.', 'textdomain' ),
            'not_found_in_trash'    => __( 'No books found in Trash.', 'textdomain' ),
            'featured_image'        => _x( 'Book Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
            'set_featured_image'    => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
            'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
            'use_featured_image'    => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
            'archives'              => _x( 'Book archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
            'insert_into_item'      => _x( 'Insert into book', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
            'uploaded_to_this_item' => _x( 'Uploaded to this book', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
            'filter_items_list'     => _x( 'Filter books list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
            'items_list_navigation' => _x( 'Books list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
            'items_list'            => _x( 'Books list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
        );
     
        $args = array(
            'labels'             => $labels,
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'rewrite'            => array( 'slug' => 'library/book' ),
            'capability_type'    => 'post',
            'has_archive'        => true,
            'hierarchical'       => false,
            'menu_position'      => null,
            'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
        );
     
        register_post_type( 'book', $args );
    }
     
    add_action( 'init', 'wpdocs_codex_book_init' );

    Note the rewrite 'rewrite' => array( 'slug' => 'library/book' ),

    Non-AMP Page and AMP page

    Thread Starter reasonx

    (@reasonx)

    Hi @milindmore22

    thank you for the reply.

    our entry for the custom post type looks similar. The rewrite is set to

    'rewrite' => array('slug' => 'testberichte', 'with_front' => false),

    but after the post-types are set up we are using a dynamic routing for posts with that post type because one part of the slug is dynamic. We are using core wordpress functions like add_rewrite_rule() and add_rewrite_tag() for this. I don′t want to put all the code of our routing classes in here but here you have the regex for the rewrite to get an idea:

    'test_amp' => new Route('^(handys)/([0-9A-Za-z\-_]+)/(test)/(amp)\s*$', array('productKind', 'urlAlias1', 'page', amp), 'ProductReviewController'),

    So the workflow is like this:

    1. A visitor requests /handys/samsung-galaxy-a53-5g/test
    2. Our Routing Class jumps in very early in the init hook and registers a rewrite rule for that specific url pattern (and others)
    3. WordPress recognizes the rule and the url and loads a specific controller class. In this case it is “ProductReviewController”
    4. Within the controller we do all sorts of things, we manipulate the query, hook on to several hooks and tell wordpress which view to load.
    5. Now that the controller is processed and the query is set up wordpress works all the way down until the post data is used in the view

    In that way it works like a charm for the non-amp version. As you can see in the regex above I have already added /amp to the path and that also works. The controller is loaded when a request to /handys/samsung-galaxy-a53-5g/test/amp is made and I can hook into pre_get_posts and set query properties like is_singular = true; and other stuff I mentioned in the initial question.

    So my assumption is that the plugin is not able to recognize this url as a valid url since it awaits the slug which is set in the post type registration (/testberichte/xxxxx/amp) but only gets /handys/xxxxxxx/test/amp.

    I was hoping we can somehow manipulate WP_Query to tell the amp plugin it can load the post as a normal amp post even if the url is beeing routed to a controller and all that stuff ?? But maybe it′s not possible at all.

    Thank you very much for your time

    Plugin Support Milind More

    (@milindmore22)

    Hey @reasonx

    The regex looks good to me, if it doesn’t work we will recommend opt-in for the default ?amp paired URL structure.

    You can learn about AMP paired URL structure and custom paired URL structure using this article

    Sorry, we can’t be more of assistance on this one!

    Thread Starter reasonx

    (@reasonx)

    Hey,

    we got it to work finally. But there is one downside. We have to put one line of code into a plugin file.

    wp-content/plugins/amp/includes/class-amp-theme-support.php:491
    $query->is_singular = true;

    That′s because $query->is_singular() is returning false if we don′t put in the line and therefore the amp plugin redirects to the non-amp version (302).

    I know it′s bad practice and we have to put the line in after every plugin update but there′s no hook in that function and it′s the only way it works at the moment so we will do it that way.

    you can close the ticket ?? Thank you for your help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Problems with custom routing’ is closed to new replies.