Comments (6)
Hello @lucasflorian, thanks for creating the issue. Would it be possible to get credentials to your website, so we could investigate this further? If so, please send them to piotr[at]wp-media.me.
from wp-rocket.
Hello, same problem here but with a more "generic" use.
We already opened a ticket 2 days ago.
We've just installed the latest version of wp-rocket (as well as wordpress and the other plugins). There seems to be an error or conflict with contact-form-7. We tested on a server with cloudflare cache, but also locally without cloudflare cache.
On pages without forms, no errors, all's well. On pages with a contact-form, I get a wpcf7 is not defined error (see screenshot). This error disappears when I deactivate the plugin. We've also checked in our code that we don't act on forms in either PHP or javascript.
Thanks
from wp-rocket.
@atoupet-toki Could you send me the URL and/or access to the staging site where we could reproduce this? piotr[at]wp-media.me
from wp-rocket.
Hello,
There is an easy way to reproduce the issue, WP Rocket must be installed as well as Contact Form 7.
If a form is added to a page while WP Rocket is enabled, in the console we can see the error wpcf7 isn't defined
.
If we do disabled WP Rocket, everything works correctly.
As @lucasflorian mentioned, from a quick check, it comes from the code he mentioned in ContactForm7.php
In fact, WPCF7 adds the CSS & JS to every pages, even tho there isn't forms. WP Rocket deletes these call, but it also doesn't add it back when it's needed.
from wp-rocket.
Reproducible with theme 2023 but not reproducible with theme 2021 v 2.0. @wordpressfan @Miraeld
from wp-rocket.
Reproduce the problem
To reproduce the issue, install and enable WP Rocket & Contact Form 7. Add a form on a page, and load that page. You'll see the error wpcf7 isn't defined
in the console.
Identify the root cause
If I understand well, we do check if the action wpcf7_enqueue_scripts
has been executed. If that's not the case, then we execute wpcf7_enqueue_scripts();
This function should load the JS file and an inline JS to be added to the page. However, in our case it's only adding the file, not the inline.
I traced back within the CF7 plugin to better understand what was happening.
The JS file is added in an easy way, however, for the inline, digging more deeply was required.
I found out it goes to their function
public function add_data( $handle, $key, $value ) {
if ( ! isset( $this->registered[ $handle ] ) ) {
return false;
}
if ( 'strategy' === $key ) {
if ( ! empty( $value ) && ! $this->is_delayed_strategy( $value ) ) {
// [...]
} elseif ( ! $this->registered[ $handle ]->src && $this->is_delayed_strategy( $value ) ) {
// [...]
}
}
return parent::add_data( $handle, $key, $value );
}
Which is interesting because of this part :
if ( ! isset( $this->registered[ $handle ] ) ) {
return false;
}
If the handle contact-form-7
isn't registered then it bails out.
And the inline script falls into that.
While retracing back how the registration occur in their plugin, I found an interesting action within CF7 plugin:
add_action(
'wp_enqueue_scripts',
static function () {
[...]
});
This one adds every registration needed.
The issue is that in ContactForm7.php we do a wpcf7_enqueue_scripts();
This function loads the JS file of CF7 but also an inline script that looks like:
<script id="contact-form-7-js-extra">
var wpcf7 = {"api":{"root":"http:\/\/localhost:8902\/wp-json\/","namespace":"contact-form-7\/v1"},"cached":"1"};
</script>
Surprisingly and without explaining exactly why on older themes, as mentioned @Mai, it was working, but it seems like on newer themes, it isn't adding the wpcf7
inline script.
Scope a solution
One solution I found to fix this issue is to add a do_action('wp_enqueue_scripts');
to ensure registration are made within CF7 plugin, before the execution of wpcf7_enqueue_scripts()
in our ContactForm7.php
file.
Effort estimation:
XS
Is a refactor needed in that part of the codebase?
No
from wp-rocket.
Related Issues (20)
- Autodelete wp-rocket-debug.log.html if it grows bigger than a specifc size
- Increase PHPStan to rule level to 1
- Warmup URLs in database not matching those in WPR debug
- Mobile manual visit isnot added to ATF table when meta viewport header is missing HOT 3
- Introduce a safe function for apply_filter
- Add section to `lcp_atf_elements` HOT 7
- Image Optimization Tab Update HOT 2
- RUCSS counter/success notices are not working correctly in certain scenario
- Preload tag has incorrect markup if LCP element has at least 2 background-images among pseudo-elements HOT 4
- Disable cache clearing when product category term is updated not working (WC REST API Request)
- Enhance the way we detect the cached page for lcp beacon script HOT 1
- Correctly handle image types when preloading LCP images HOT 16
- 3.16: Hebrew letters break preload URL HOT 4
- Improve integration tests using function mock instead of HTTP short-circuit
- Enable mobile cache button is not working in some cases HOT 2
- WP-Rocket/SaaS Crawl speed too fast HOT 2
- LCP is detected with wrong href coming from a url(xxx) syntax HOT 4
- Automatically build JS/CSS assets of WP Rocket plugin on develop branch
- Optimize the way we fetch links on home page for warmup HOT 1
- ATF optimization and lazyload exclusion is not always applied when using a CDN HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wp-rocket.