WordPress: How to Add CSS to a Specific URL

This is an interesting topic. A WordPress/WooCommerce client could not add CSS via the usual way. In fact, Gravity Forms plugin (and in particular the “directory” extension), stores entries in the database BUT the directory page and the entries filtered list have the same classes, ids, and cannot be “targeted” with CSS.

Long story short, and Gravity Form or not Gravity Form, here’s the snippet you can use to add CSS to any WordPress page as long as you know its URL.

Let’s say I want to apply some red font to the page title if the URL contains the string “yo”. All I need is the snippet below, where I use “yo” as the search string. That’s it!

PHP Snippet: Apply CSS to a WordPress URL That Contains “abc”

/**
 * @snippet       Apply CSS if URL contains "abc" - WordPress
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_action( 'init', 'bbloomer_apply_css_if_url_contains_string' );
 
function bbloomer_apply_css_if_url_contains_string() {
   $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
   if ( false !== strpos( $url, 'abc' ) ) {
      echo '<style>.entry-title { color: red }</style>';
   } 
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Add CSS to Order Emails
    Unlike your WordPress theme, you can’t just add CSS to your style.css in order to customize the look of the WooCommerce emails. This handy PHP snippet is therefore the only viable solution. It’s a little tricky but once you get the idea, adding CSS to Order Emails is a breeze.
  • WooCommerce: Change the “Remove this Item” Icon @ Cart
    I have a very long to-write list. Possibly I have enough content for another 2 years ๐Ÿ™‚ However, the other day a premium WooCommerce student asked me for some feedback on his custom CSS – so I had to give it priority! The request was: what’s the easiest way to change the little icon/button on […]
  • WooCommerce: Product Category as Body Class @ Single Product Page
    A Business Bloomer fan asked me an interesting question this week: how to apply CSS on the single product page based on the product category? Well, the answer is pretty simple: if we’re able to add the category name to the HTML “body”, this can then be targeted in your custom CSS. So, let’s see […]
  • WooCommerce and Divi: how to override !important CSS
    I am a big fan of Elegant Themes, but a lot of CSS styles (mainly in Divi) contain “!important” in order to override WooCommerce styles when using the two in a combo. Now, the big problem is that we can’t override !important with another !important. Divi will win. But no matter whether Divi has been […]
  • WooCommerce: Full Width Featured Image @ Single Product Page
    The standard layout for the WooCommerce single product page features the main/featured product image on the left and the title/add to cart on the right. But what if you need to turn that image into a hero one i.e. a full width featured image, and push the title and add to cart button under it? […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

25 thoughts on “WordPress: How to Add CSS to a Specific URL

  1. Hi! I should add the domain name to “SERVER NAME” and the path to “SERVER URI”??

    1. Sorry for the confusion. The only thing you need to change is the “abc” search string, so if your URL is example.com/whatever you need to replace “abc” with “whatever“, and then write the CSS that you want to apply. That’s it!

  2. NIIIIIIce thanks man!

  3. Boom!!!! You are the man!
    Thank you so much. I had a Javascript instead if this amazing function.
    I just added this to load it in the head.

    function inline_css() {
      echo "<style>.html{ color: red } }</style>";
    }
    add_action( 'wp_head', 'inline_css', 0 );
    } 
    

    Thanks

  4. A very interesting thing happened when I applied the code. I used it for the WooCommerce My Account sub-page “orders” just to change an Elementor element I put above the my account shortcode. It broke the WooCommerce orders table, but only on mobile Safari and mobile Chrome and only the “display: block” attribute did not work anymore. When I changed the font of the table the CSS showed, but the “display” attribute is somehow broken. When I looked at it in Chromes inspect window with mobile view the table was normal. Very odd behaviour. On other pages with tables everything works fine when I use this code. Do you maybe know why the “display” attribute won’t work on the WooCommerce order table because of this code?

    1. It depends on many things, and first of all on the CSS you applied. Want to share it here maybe?

      1. Sure. I applied CSS to a row above the orders content from Elementor (which I made to as a navigation through the My Account part), to change the background-color and color of the “buttons” when the specific URL is activated. (you can see it here what happens, if the php code is applied: https://www.magentacloud.de/lnk/TwCFjgXz#file)

        I applied this code:

        add_action( 'init', 'bbloomer_apply_css_if_url_contains_bestellungen' );
          
        function bbloomer_apply_css_if_url_contains_bestellungen() {
          
        $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
          
        if ( false !== strpos( $url, 'bestellungen' ) ) {
            echo '<style type="text/css">
                 .elementor-11 .elementor-element.elementor-element-23f25a3 > .elementor-widget-container {background-color:#000 !important} .elementor-11 .elementor-element.elementor-element-23f25a3 .elementor-icon-box-content .elementor-icon-box-title {color:#fff !important} .elementor-11 .elementor-element.elementor-element-23f25a3 .elementor-icon i {color: #fff;} .woocommerce-page.table.shop_table_responsive tr td {display:block !important}
                 </style>';
        } 
          
        }
        
  5. This did not work for me – it put the resulting style at the very top of the html output – before the

    1. Where did you place your CSS?

  6. Hi! I used this snippet for changing CSS on specific pages where i need. It works perfect BUT:
    Can I ask if using it multiple times in a short period gets the Imunify360 to block the ip?
    Because that does happen, it changes the CSS of said page and nothing brakes, so before I freak out again I just want to make sure it’s just a safety measure and that I shouldnt worry? Or should I proceed in another way?

    1. Hi Vanessa, no it shouldn’t

  7. Hi, just wanted to let you know that I used something similar ages ago and wanted to use that code again and I was having a blank/couldn’t remenber the exact coding I did, cause I went withouth coding for years, bla bla bla.
    TL;DR.
    Ur example here is exactly what I used to do and I want to let other users know that it works like a charm.

    1. Fantastic ๐Ÿ™‚

  8. Excellent PHP snippet, i changed “strpos” by “strripos” to apply it to last segment of an url
    and it’s working!
    Thank you

  9. Thank you so much. This is such a big help. You are awesome!!!

  10. Thank you its work’s great !!

  11. hello sir,
    What if i want to call class or id of particular division on that page?

    1. Chandan, thanks for your comment! I have no idea I’m afraid, I will need a URL and more information ๐Ÿ™‚ Thanks!

  12. thank you, this solved a huge problem for me

    1. Awesome, thanks Todd!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *