WordPress: How to Add CSS to a Specific Page 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.

PHP Snippet: Apply CSS to a Page URL That Contains “string”

 * @snippet       Apply CSS if URL contains string - WordPress
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.7
 * @donate $9     https://businessbloomer.com/bloomer-armada/

add_action( 'init', 'bbloomer_apply_css_if_url_contains_string' );
function bbloomer_apply_css_if_url_contains_string() {
if ( false !== strpos( $url, 'string' ) ) {
    echo '<style type="text/css">
         .entry-title { color: red }

Was this article helpful?

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

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

  1. NIIIIIIce thanks man!

  2. 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 );


  3. 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}
  4. 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?

  5. 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

  6. 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.
    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 ๐Ÿ™‚

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

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

  9. Thank you its work’s great !!

  10. 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!

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

    1. Awesome, thanks Todd!

Questions? Feedback? Support? Leave your Comment Now!

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

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