Enqueue and load a CSS file *only* if string in content matches?
This is a ‘writing a plugin’ question.
I have a CSS and JS file that I only want to include (as they’re quite big) if any post on the current page (index, single, search, etc) contains a specific shortcode. To simplify, let’s say I want to only load it if the post content contains “FOOBAR”. Of course, I’d like to use wp_enqueue_style() and wp_enqueue_script() – the proper way to do it.
Is this possible?
The problem is that the header is printed before the content, so as far as I understand it’ll be too late. If wordpress made 2 passes – the first one to determine everything and run all the hooks, and the second one to print everything, it’d be easier.
The wp_print_styles hook is triggered outside of the content loop and is just a hook, not a filter. the_content filter is already too late, as far as I can tell.
Any ideas, guys? This is a really common optimization a lot of plugins could use (don’t trash every page with your css/js unless needed).
- The topic ‘Enqueue and load a CSS file *only* if string in content matches?’ is closed to new replies.