Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optimization for Contact Form 7 #6215

Merged

Conversation

adamsilverstein
Copy link
Contributor

@adamsilverstein adamsilverstein commented Oct 10, 2023

Only load Contact Form 7’s JavaScript if a form is present on the page.

Description

The Contact Form 7 ("CF7") specific optimization ensures that CF7 front end scripts are only enqueued when the page contains a contact form.

fixes #6241

Type of change

  • Use CF7 hooks and functions to optimize sites using CF7 as recommended by their docs
  • Reduce excessive script loading for pages without contact forms, for example if the contact form is only used on a "Contact Us" page

Is the solution different from the one proposed during the grooming?

Please describe in this section if there is any change to the groomed solution, and why.

Checklists

Generic development checklist

  • My code follows the style guidelines of this project, with adapted comments and without new warnings.
  • I have added unit and integration tests that prove my fix is effective or that my feature works.
  • The CI passes locally with my changes (including unit tests, integration tests, linter).
  • Any dependent changes have been merged and published in downstream modules.
  • If applicable, I have made corresponding changes to the documentation. Provide a link to the documentation.

Test summary

  • Not yet tested

If not, detail what you could not test.

Please describe any additional tests you performed.

engahmeds3ed and others added 9 commits August 14, 2023 06:13
Release v3.15.0.1
Onload load Contact Form 7’s JavaScript if a form is present on the page.
…lugin

* Use template_redirect action rather than wp to prevent accidental admin usage.
* Add version check for CF7.
* Add conditional enqueueing for styles in addition to scripts.
* Override filters for wpcf7_load_js and wpcf7_load_css to return false.
* Prevent re-enqueueing scripts after they have been enqueued.
@westonruter westonruter force-pushed the add/contact-form-7-optimization branch from 6284813 to 6ece016 Compare October 10, 2023 20:08
@westonruter
Copy link
Contributor

Note this PR incorporates optimizations from a standalone mini plugin. This was developed to fix rocklobster-in/contact-form-7#1278 in which scripts and styles for CF7 are enqueued on every page even when there is no contact form present.

@adamsilverstein adamsilverstein marked this pull request as ready for review October 13, 2023 15:10
@Mai-Saad Mai-Saad self-requested a review November 27, 2023 09:29
Copy link
Contributor

@Mai-Saad Mai-Saad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working as expected and js/css isn't loaded now unless it's used.

Copy link
Contributor

@wordpressfan wordpressfan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @adamsilverstein , this is a great honor working with u on the same PR.

We will merge this PR but we need to revisit it to add tests and also change it a bit to have the callbacks registering inside the method get_subscribed_events after adding the bailout conditions there and small code formatting concepts that we use here like bailout early.
But the PR itself is good and working perfectly.

@wordpressfan wordpressfan added this pull request to the merge queue Nov 29, 2023
Merged via the queue into wp-media:develop with commit f588be9 Nov 29, 2023
3 of 8 checks passed
@adamsilverstein
Copy link
Contributor Author

Thanks everyone for the attention on this, great to see this merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add optimization for Contact Form 7
6 participants