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”
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.
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!
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”
NIIIIIIce thanks man!
Boom!!!! You are the man!
I just added this to load it in the head.
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?
It depends on many things, and first of all on the CSS you applied. Want to share it here maybe?
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:
This did not work for me – it put the resulting style at the very top of the html output – before the
Where did you place your CSS?
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?
Hi Vanessa, no it shouldn’t
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.
Excellent PHP snippet, i changed “strpos” by “strripos” to apply it to last segment of an url
and it’s working!
Thank you so much. This is such a big help. You are awesome!!!
Thank you its work’s great !!
What if i want to call class or id of particular division on that page?
Chandan, thanks for your comment! I have no idea I’m afraid, I will need a URL and more information 🙂 Thanks!
thank you, this solved a huge problem for me
Awesome, thanks Todd!