Some issues with our compatibility file for Contact Form 7 #6384
Labels
3rd party compatibility
Issues related to 3rd party compatibility like theme, plugin or hosting
needs: documentation
Issues which need to create or update a documentation
needs: grooming
priority: high
Issues which should be resolved as quickly as possible
severity: major
Feature is not working as expected and no work around available
type: bug
Indicates an unexpected problem or unintended behavior
Milestone
I checked two escalated cases and found out some issues with Contact Form 7 so I'll add the full details here so we can take a decision with the team if we want/need to do any enhancement here or have a documentation for the following points but I need to give you a small intro that most of those points are depending on, which is:
When you register a script without enqueuing it and then enqueue another script that has the first script as a dependency, this will force enqueuing the first one, let's check the code of CF7 to see this in action:
Start here, they register the script
contact-form-7
https://github.com/rocklobster-in/contact-form-7/blob/v5.8.6/includes/controller.php#L49-L58
and only enqueue it inside the following function:
https://github.com/rocklobster-in/contact-form-7/blob/v5.8.6/includes/controller.php#L110
and this function is called when the filter
wpcf7_load_js
is applied here:https://github.com/rocklobster-in/contact-form-7/blob/v5.8.6/includes/controller.php#L68-L70
So the issue happens when a script is enqueued but has the
contact-form-7
script ID as a dependency, this means that CF7 script will be enqueued directly without checking this filter so this may cause some errors with our compatibility file as below:Console error in pages that don't have forms when having
Conditional Fields for Contact Form 7
plugin activeWhen
Conditional Fields for Contact Form 7
is active we see a console error as below:and this is happening only for the pages that don't have contact forms.
and after checking their code I found that they start enqueuing their scripts here:
https://plugins.trac.wordpress.org/browser/cf7-conditional-fields/tags/2.4.6/cf7cf.php#L510
then calling their function
wpcf7cf_enqueue_scripts
then here:https://plugins.trac.wordpress.org/browser/cf7-conditional-fields/tags/2.4.6/cf7cf.php#L524
they have CF7 script as a dependency, so as u can see they have a function called
wpcf7cf_load_js
which applies the filterwpcf7cf_load_js
So we need to use the following filters to control the loading of their scripts that causes CF7 scripts to be enqueued too:
we can updated our compatibility file here:
wp-rocket/inc/ThirdParty/Plugins/ContactForm7.php
Line 6 in 0484f52
with every instance of
wpcf7_load_js
we need to do that same with the filterwpcf7cf_load_js
, for example here:wp-rocket/inc/ThirdParty/Plugins/ContactForm7.php
Lines 36 to 37 in 0484f52
we need to add the following two lines:
and the same needs to be done below in the same file, or we can create another compatibility for that.
Conflict when custom themes that is adding custom scripts that works with CF7
I found a customer that has the following code in his custom theme:
this code will also force enqueuing CF7 script even when not needed (without checking if CF7 should be enqueued or not)
This can be fixed by replacing the action from
wp_enqueue_scripts
to bewpcf7_enqueue_scripts
which only fires when CF7 scripts are being loaded.Enqueuing CF7 directly is a problem because it will load the script only without adding the localized script
wpcf7
which is a requirement for the CF7 script.So the question is, why this is happening only with our compatibility file?
That's a good question, the answer is that by default the filters
wpcf7_load_js
andwpcf7_load_css
return true so the script will always be enqueued along with the localized script.But our compatibility file is changing their return based on if the current page contain a contact form or not, and if case of
Not
the localized script won't be added even if a third party enqueued CF7 script which causes console errors and corrupted functionality.So I believe we are not wrong, the wrong comes from the third parties that need to add some condition before loading their scripts.
We need a product decision here on what should we do next.
@wp-media/productrocket
The text was updated successfully, but these errors were encountered: