Conditional loading of scripts/styles
-
Right now, the only documented way of turning off scripts/styles is by setting a PHP constant. This is kind of problematic, as if you’re using version control, you won’t be committing wp-config.php, and the next time you set up the site from the repo, the scripts will once again be loaded.
I noticed you do have a filter that can be used, however, so I’ve gone that route:
add_filter('wpcf7_load_css', function() { return false; });
Same for scripts. I think a lot of people would benefit if you documented that.
But the crux here is coming up with a way to improve the plugin permanently. In order to load the scripts properly on the pages that need them after using the filter above, I have to put a filter on specific pages I know will have forms or something, via template or otherwise, which isn’t ideal.
I’m imagining there’s some consistent entry point for form rendering – something like the
form_html
function. It’d be great if this entry point had an action I could hop onto to get the scripts loading again when needed. Something like:do_action('wpcf7_form_rendered', function() { wp_enqueue_script('contact-form-7'); });
This would, of course, require you registering the script somewhere higher in the chain before enqueuing it – that way the handle is available to anyone.
This is just one approach, and in the end, I think I’d just like to see, out-of-the-box, this plugin not loading 4 scripts on pages without contact forms.
- The topic ‘Conditional loading of scripts/styles’ is closed to new replies.