Add the following code to your theme or plugin to load the scripts and styles only on pages that have a contact form.
7 thoughts on “Prevent Contact Form 7 from loading on every page”
There is even a better way to obtain this, that prevents the script to be added and then removed.
Have a look here:
Hey Andrea, thanks for sharing this. I’m not sure if this way existed when I wrote this post. I don’t think it’s “better” necessarily, just different. In cases where you don’t have a template specifically for contact and where many pages share the same page template, the method I outline above keeps it loading only when necessary.
The next time I need to use CF7 I’ll take a closer look at this and improve upon it if necessary.
HI John, thanks for sharing this. However, the ‘contact’ in your example is page name or …something else?
‘contact’ is the page slug, which in this example would be domain.com/contact. `is_page` will accept the page ID, slug, or page title. See these examples: https://codex.wordpress.org/Function_Reference/is_page
Thanks for your clarification =)
Is it possible for it to smart load only when their shortcode is used. It’s seems crazy that it loads on all pages. Why doesn’t it jus tload on shortcode use.
I don’t want to use this function as I won’t always know which pages the form is used on
Hey Daniel, I haven’t looked at this in quite some time. I’m not sure if they’ve improved the script loading in the latest versions or not. Hopefully by now they have a better way. When I find some time I’ll look into it and update this post if necessary.