Skip to content

Commit

Permalink
Add code
Browse files Browse the repository at this point in the history
  • Loading branch information
PasanBhanu committed Dec 28, 2024
1 parent 0cb4110 commit 38e9e7b
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 2 deletions.
94 changes: 92 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,92 @@
# contact-form-7-recaptcha-limiter
Limit reCaptcha Loading Only to Pages with Contact Form 7 Shortcodes
# Contact Form 7 reCAPTCHA Limiter

**Contact Form 7 reCAPTCHA Limiter** is a WordPress plugin that ensures the reCAPTCHA scripts and styles are loaded only on pages containing Contact Form 7 forms. This improves performance and eliminates unnecessary reCAPTCHA assets on other pages.

---

## Features

- Limits reCAPTCHA scripts and styles to pages with `[contact-form-7]` shortcodes.
- Prevents unnecessary loading of Google reCAPTCHA resources on unrelated pages.
- Improves site performance by reducing the number of globally loaded scripts.

---

## Requirements

- WordPress 5.0 or higher
- Contact Form 7 plugin installed and active
- PHP 7.0 or higher

---

## Installation

### 1. Download and Install
1. Download the plugin files from the repository or your zip archive.
2. Extract the files and upload the folder `contact-form-7-recaptcha-limiter` to the `/wp-content/plugins/` directory.

### 2. Activate the Plugin
1. Go to the **Plugins** section of your WordPress admin panel.
2. Find "Contact Form 7 reCAPTCHA Limiter" in the list and click **Activate**.

---

## Usage

The plugin automatically handles the reCAPTCHA script management. Once activated:
1. Visit any page with a Contact Form 7 form containing the `[contact-form-7]` shortcode, and reCAPTCHA will load.
2. Other pages without the shortcode will not load the reCAPTCHA scripts or styles.

---

## How It Works

1. The plugin detects pages with the `[contact-form-7]` shortcode.
2. It ensures that the `google-recaptcha` and `wpcf7-recaptcha` scripts are only enqueued on those pages.
3. Other pages remain unaffected, improving load time and reducing unnecessary script calls.

---

## FAQ

### **Why do I need this plugin?**
By default, Contact Form 7 loads reCAPTCHA scripts and styles globally on all pages, which can impact site performance. This plugin prevents that and ensures reCAPTCHA is only loaded when required.

### **Does this plugin modify Contact Form 7?**
No, it works as an add-on to Contact Form 7 without modifying its core files.

### **Will this plugin affect other functionality of Contact Form 7?**
No, it only affects the loading behavior of reCAPTCHA scripts and styles. Other functionalities remain intact.

### **Is this plugin compatible with caching plugins?**
Yes, but ensure you clear your cache after activating or deactivating the plugin.

---

## Support

For any issues or feature requests, please [open an issue](https://github.com/your-repository-link/issues) or contact the plugin author.

---

## License

This plugin is licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). See the `LICENSE` file for more details.

---

## Contribute

Contributions are welcome! If you'd like to contribute:
1. Fork the repository.
2. Create a feature branch.
3. Submit a pull request.

---

### Example Page Setup

Add a Contact Form 7 shortcode to your page content:
```html
[contact-form-7 id="123" title="Contact form 1"]
24 changes: 24 additions & 0 deletions contact-form-7-recaptcha-limiter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/*
Plugin Name: CF7 reCAPTCHA Limiter
Plugin URI: https://softinklab.com
Description: Limits reCAPTCHA loading to pages containing Contact Form 7 forms.
Version: 1.0
Author: Pasan Guruge
Author URI: https://pasanbhanu.me
License: Apache 2.0
License URI: https://www.apache.org/licenses/LICENSE-2.0
*/

// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}

// Define constants
define('CF7_LIMITER_PATH', plugin_dir_path(__FILE__));
define('CF7_LIMITER_URL', plugin_dir_url(__FILE__));

// Include additional functionality
require_once CF7_LIMITER_PATH . 'includes/functions.php';
?>
16 changes: 16 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
function cf7_limiter_manage_recaptcha_assets() {
global $post;

// Check if the current page contains the CF7 shortcode
$has_contact_form = is_singular() && isset($post->post_content) && has_shortcode($post->post_content, 'contact-form-7');

// Dequeue scripts and styles if the page doesn't contain the shortcode
if (!$has_contact_form) {
wp_dequeue_script('google-recaptcha');
wp_dequeue_script('wpcf7-recaptcha');
wp_dequeue_style('wpcf7-recaptcha');
}
}
add_action('wp_print_scripts', 'cf7_limiter_manage_recaptcha_assets', 100);
?>

0 comments on commit 38e9e7b

Please sign in to comment.