• Resolved johnbenedict

    (@johnbenedict)


    Our website uses a custom permalink setup that goes something like this: Base URL > State based on Manual Input > Name (CPT) > which has a custom gallery of photos, each with their own permalink — so the structure of that link is base_url/state/name/photo_name

    Out of the box — everything on our site works with the amp plugin — except for the photo pages. In order to fix those — we had a developer come up with a solution.

    Unfortunately — that solution modified the AMP Plugin. I’ve been trying to find an alternative solution — such as moving this fix to our functions.php file — but so far no luck. I even consulted with ChatGPT (offered but non-working solutions below).

    I have a strong feeling a dev familiar with this plugin could knock this out no problem. Thank you in advance for anyone willing to take a stab at helping me out with this.

    Here is our current working solution:

    The file we edited is: https://github.com/ampproject/amp-wp/blob/develop/src/MobileRedirection.php

    The edit we made is:

    public function get_current_amp_url() {
    
    $url = $this->paired_routing->add_endpoint( amp_get_current_url() );
    
    $url = remove_query_arg( QueryVar::NOAMP, $url );
    
    # FIX
    
    $sep = "&";
    
    if (stristr($url, '?') === false) $sep = '?';
    
    foreach ($_GET as $k => $v) {
    
    $url .= $sep . urlencode($k) . "=" . urlencode($v); #$k=$v";
    
    $sep = "&";
    
    }
    
    return $url;
    
    }

    ChatGPT Solution 1:

    I see that you made an edit to the get_current_amp_url() method in the MobileRedirection.php file of the AMP for WordPress plugin. The change you made appends the current query parameters to the generated AMP URL.

    To move this fix to your functions.php file, you can use the amp_post_template_data filter provided by the AMP for WordPress plugin. You can add this code to your theme’s functions.php file:

    function custom_amp_current_amp_url($data, $post) {
        $current_amp_url = $data['amphtml'];
        
        $sep = "&";
        if (strpos($current_amp_url, '?') === false) {
            $sep = '?';
        }
        
        foreach ($_GET as $key => $value) {
            $current_amp_url .= $sep . urlencode($key) . '=' . urlencode($value);
            $sep = '&';
        }
        
        $data['amphtml'] = $current_amp_url;
        
        return $data;
    }
    
    add_filter('amp_post_template_data', 'custom_amp_current_amp_url', 10, 2);

    This code hooks into the amp_post_template_data filter, which allows you to modify the AMP URL. It replicates the same functionality you added to the plugin code by appending query parameters to the AMP URL.

    After adding this code to your functions.php, the fix should work as intended, and you won’t need to edit the plugin code directly. Make sure to clear any caching, if you are using a caching plugin, for the changes to take effect.

    ChatGPT Solution 2:

    I apologize for the inconvenience. If the code I provided isn’t working as expected, let’s try another approach. You can override the get_current_amp_url method in your theme’s functions.php file. Here’s how to do it:

    Open your theme’s functions.php file.

    Add the following code:

    function custom_get_current_amp_url($url) {
        $sep = '&';
        if (strpos($url, '?') === false) {
            $sep = '?';
        }
    
        foreach ($_GET as $key => $value) {
            $url .= $sep . urlencode($key) . '=' . urlencode($value);
            $sep = '&';
        }
    
        return $url;
    }
    
    add_filter('amp_get_current_url', 'custom_get_current_amp_url', 10, 1);

    This code hooks into the amp_get_current_url filter and modifies the AMP URL in the same way you were doing in the plugin.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Permalink Setup’ is closed to new replies.