In your example, $_SERVER['REQUEST_URI']
would return /category/post-slug/amp/
which is not the same as get_permalink($post->ID)
which would return https://example.com/category/post-slug/
There are lots of ways to go about this, but one easy one that comes to mind is…
if ( strpos($_SERVER['REQUEST_URI'], '/amp/' ) !== false ) {
The strpos()
PHP function finds the position of 2nd string within the 1st string, and returns false if it’s not found.
So, that essentially searches the Request URI for the existence of ‘/amp/’ and if it’s found, then it proceeds with the rest of our function.
Related Posts:
- Passing and retrieving query vars in wordpress
- Get the current page URL (including pagination)
- What is the difference between get_post_permalink and get_permalink?
- WordPress matching URLs with trailing tildes
- Why is style.css not being enqueued?
- Setting up WordPress with Custom Permalinks and no .htaccess File?
- How to use relative links on my pages?
- mod_rewrite enabled but Permalinks show index.php
- Why does /wp-json/ not work on the “plain” permalink structure?
- Custom Permalink: /year/month/post-goes-here/?
- Menu Link Redirect Based on Username or User ID
- Turn off %postname% auto-incrementing?
- How to get the post id from a permalink?
- Redirect short-form URL to long-form URL (post_id to post_id + post_name)
- Does a parent page’s post_title have to appear in a child page permalink?
- Enable Minify in W3Total Cache using NGINX [closed]
- Stop redirecting to posts not in a category / duplicate slugs
- How to make variables in URL look like the permalink structure?
- Why I can’t change the permalink of this page?
- Custom Taxonomy URL
- Register script/style: Is it possible to customize the version query string via plugin?
- WordPress not respecting template hierarchy (fetches index.php instead of single.php or page.php)
- Why does chrome keep downloading a file instead of running the site?
- How can I add a page’s ID to its permalink using WP_Rewrite?
- Re-write specific custom post type category URL to go to another page
- How to change the permalink option to post name on theme activation?
- Does rename of page slugs create 301 redirects from old slug to new (same as for posts)?
- Changing post content on save, using updated fields
- Change blog segment of URL
- Change in permalink structure made javascript cookie not work correctly
- Change permalinks on move from one site to another
- Making permalinks to /%postname%/ by default in installation
- Images in media library have suddenly disappeared
- Post with Custom Permalinks
- Can’t select blog month within archive category
- Passing the JQuery string to Worldpress URL
- An other permalink problem – 404 Error
- permalink results to “page not found”
- permalinks issue and archives
- get_permalink() always returns empty
- How to prevent WP overwriting my custom htaccess rewrites?
- WordPress basic functions show on top in custom shortcode
- Internal linking when on a shared IP
- Edit permalink for certain pages
- How can I include permalinks in this code?
- How can I redirect this contact form to a specific permalink
- Strange behavior with a redirect loop – involves WPML multi-language plugin
- Adding category slug to posts permalink causes 404 error
- http: not showing in wordpress permalink setting
- LAMP WordPress Permalinks 404 error
- Links in post content don’t work on home page
- Remove all stop words from old permalinks
- Arabic permalink redirect to home page
- Changing permalink settings causes 404
- 404 error on all pages but custom post types are working OK
- Deregister a CSS file that comes with a plugin
- How can I see a list of all pages my WordPress site has generated?
- Can I change the URL structure for a CPT only?
- Custom permalink structure for product
- Make a post inaccessible without a unique ID in the URL
- index.php in URL
- archive by year and category: permalink support doesn’t work on pagination
- Empty the_excerpt() broke the_permalink() under him?
- Code snippet to add filter pre_post_link to change Media URL (post_type attachment permalink/slug)
- Permalink not changing according to post type
- iFrame permalinks on WordPress
- Change permalink structure in all sites of a multisite
- Custom permalink structure with a prefix just for posts
- Changing permalink stops me from creating webpages
- Make query parameter an endpoint
- 404 page not found – Yet open admin page it works
- How can I have a page’s permalink include my custom post type’s slug?
- Added a prefix in URL permalink structure but the old structure still working!
- WordPress Permalinks
- Problem with permalinks
- Find internal links to post
- “Undefined” added to page content after google Adwords click
- create user’s view profile link with username
- Image upload ruined my links
- How to rewrite a folder in WordPress?
- The Permalink doesn’t work after the foreach ended
- Force/Limit the post/page slug to accept only url friendly characters
- Permalinks setting raises 500.0 error
- Define a Permalink Structure by Post and Page
- Tag search not working after permalink structure change
- After changing permalink settings, everything works fine except for the two most recent posts
- permalink repeated
- .htaccess not working in WordPress
- Custom permalink to make a page respond properly
- How to avoid duplicate Url for the home page
- WordPress 3.1 problem, getting 404
- Display a custom slug in URL depending on user variable
- htaccess strip url
- Problems loading stylesheet and permalinks after rebooting
- How to create my own style.css file in an wordpress child-theme
- Error: “Sorry, this content could not be embedded” when using the Embed block
- WordPress Permalinks 404 Not Found
- WordPress not remembering old slugs for pages
- make a new structure tag that does some processing
- Website appears with wrong permalink in Google’s search