0

I applied a filter to remove all theme styles, but I want this to happen only if the url ends with /amp/.

The code below is removing the .css styles on all pages, not filtering the term /amp/ of the permalink.

I am using the AMP for Wordpress plugin, which adds /amp/ at the end of the URL. Examplo: mysite.com/category/post-slug/amp/

function remove_all_theme_styles() {
if ( get_permalink($post->ID) . '/amp/' ) {
    global $wp_styles;
    $wp_styles->queue = array();
}
}
add_action('wp_print_styles', 'remove_all_theme_styles', 100);

They said to add, but neither is working.

function remove_all_theme_styles() {
if ( $_SERVER['REQUEST_URI'] == get_permalink($post->ID) . '/amp/' ) {
  • Are you using an Amp plugin? Or is this a homegrown solution? Are you using a page endpoint? Or pages named /amp? Please edit your question with the needed information – Tom J Nowell Aug 28 '18 at 2:06
  • The question was updated. – BóbGCA Aug 28 '18 at 2:51
1

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.

  • not sure the usage of strpos is 100% in place. While it is most likely good enough to 99% of sites, there must at least 1 that uses amp as category or page slug, and the detection will fail for it – Mark Kaplun Aug 28 '18 at 4:59
0

Add this function somewhere (Rename to something you wish)

function force_relative_url ($url) {
  return preg_replace ('/^(http)?s?:?\/\/[^\/]*(\/?.*)$/i', '$2', '' . $url);
}

Now you can:

if ( $_SERVER['REQUEST_URI'] == force_relative_url( get_permalink( $post->ID ) ) . '/amp/' ) {
0

Based on the plugin you're using, take a look at its is_amp_endpoint() function.

You may perform the test this way in another plugin on your theme's functions.php:

if( function_exists('is_amp_endpoint') && is_amp_endpoint() ) {
    // ... do stuff in AMP
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.