Skip to content

Commit 9a850d9

Browse files
committed
Initial commit
0 parents  commit 9a850d9

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Plugin Name: Conditional Smart Tags for WPForms
4+
* Plugin URI: https://wordpress.org/plugins/conditional-smart-tags-for-wpforms
5+
* Description: Adds a custom if-then-else smart tag syntax to conditionally show or hide content based on a field value in WPForms.
6+
* Version: 1.0.0
7+
* Requires at least: 5.2
8+
* Requires PHP: 7.2
9+
* Author: Maximum.Software
10+
* Author URI: https://maximum.software
11+
* Text Domain: conditional-smart-tags-for-wpforms
12+
* License: GPL-2.0-or-later
13+
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
14+
*/
15+
16+
// Exit if accessed directly.
17+
if( ! defined( 'ABSPATH' ) )
18+
exit;
19+
20+
class WPForms_Conditional_Smart_Tags
21+
{
22+
/**
23+
* Constructor to hook into WPForms smart tag processing.
24+
*/
25+
public function __construct()
26+
{
27+
add_filter( 'wpforms_process_smart_tags', [ $this, 'process_if_else_smart_tags' ], 10, 4 );
28+
}
29+
30+
/**
31+
* Callback to parse {if field_id="X" value="Y"} ... {else} ... {/if}.
32+
*
33+
* @param string $content The content (notification, confirmation) containing smart tags.
34+
* @param array $form_data Data about the current form.
35+
* @param array $fields Submitted fields data.
36+
* @param int $entry_id The current entry ID.
37+
*
38+
* @return string Processed content with if/else blocks replaced accordingly.
39+
*/
40+
public function process_if_else_smart_tags( $content, $form_data, $fields, $entry_id )
41+
{
42+
$max_iterations = 10; // Prevent infinite loops
43+
$iteration = 0;
44+
45+
while(strpos($content, '{if') !== false && $iteration < $max_iterations)
46+
{
47+
$content = $this->process_innermost_if($content, $fields);
48+
$iteration++;
49+
}
50+
51+
return $content;
52+
}
53+
54+
/**
55+
* Process the innermost if statement in the content.
56+
*
57+
* @param string $content The content to process
58+
* @param array $fields The form fields
59+
* @return string Processed content
60+
*/
61+
private function process_innermost_if($content, $fields)
62+
{
63+
// Find the innermost {if} that doesn't contain another {if}
64+
$pattern = '/\{if\s+field_id="(\d+)"\s+value="([^"]*)"\}((?:(?!\{if).)*?)(?:\{else\}((?:(?!\{if).)*?))?\{\/if\}/s';
65+
66+
return preg_replace_callback($pattern, function($matches) use ($fields) {
67+
$field_id = $matches[1];
68+
$required_value = $matches[2];
69+
$if_content = $matches[3];
70+
$else_content = isset($matches[4]) ? $matches[4] : '';
71+
72+
// Get the submitted value, if present
73+
$submitted_value = '';
74+
if(isset($fields[$field_id]['value']))
75+
$submitted_value = trim($fields[$field_id]['value']);
76+
77+
// Compare and return if_content or else_content
78+
return ($submitted_value === $required_value) ? $if_content : $else_content;
79+
}, $content);
80+
}
81+
}
82+
83+
$wpfcst_instance = new WPForms_Conditional_Smart_Tags();

readme.txt

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
=== Conditional Smart Tags for WPForms ===
2+
Version: 1.0.0
3+
Stable tag: 1.0.0
4+
Requires at least: 5.2
5+
Tested up to: 6.7
6+
Requires PHP: 7.2
7+
Tags: conditional logic, conditional fields, wpforms, smart tags, forms
8+
Plugin URI: https://wordpress.org/plugins/conditional-smart-tags-for-wpforms
9+
Author: Maximum.Software
10+
Author URI: https://maximum.software
11+
License: GPL-2.0-or-later
12+
License URI: https://www.gnu.org/licenses/gpl-2.0.html
13+
14+
Adds a custom if-then-else smart tag syntax to conditionally show or hide content based on a field value in WPForms.
15+
16+
== Description ==
17+
18+
Conditional If/Then/Else Smart Tags for WPForms enhances your form by adding conditional logic capabilities through smart tags. This plugin allows you to display different content based on form field values.
19+
20+
= Features =
21+
22+
* Add if/then/else conditions where smart tags can be used
23+
* Compare field values using exact match conditions
24+
25+
= Example Usage =
26+
27+
Use this syntax in your WPForms notifications or confirmations:
28+
29+
`{if field_id="3" value="TestValue"}
30+
This message is shown ONLY if field #3 exactly matches "TestValue".
31+
{else}
32+
This message is shown otherwise.
33+
{/if}`
34+
35+
== Installation ==
36+
37+
1. Upload the plugin files to the `/wp-content/plugins/conditional-smart-tags-for-wpforms` directory, or install the plugin through the WordPress plugins screen directly.
38+
2. Activate the plugin through the 'Plugins' screen in WordPress
39+
3. Use the new if/then/else smart tags in your WPForms notifications and confirmations
40+
41+
== Frequently Asked Questions ==
42+
43+
= How do I find a field's ID? =
44+
45+
The field ID can be found in the WPForms form builder. When editing a field, look for the "Field ID" in the field options panel on the left.
46+
47+
= Can I use multiple conditions? =
48+
49+
Yes, you can use multiple if/then/else blocks. Just make sure to properly close each if block with {/if}.
50+
51+
= Can I nest if/then/else blocks and other smart tags? =
52+
53+
Yes, you can use nested if/then/else blocks and other smart tags.
54+
55+
== Changelog ==
56+
57+
= 1.0.0 =
58+
* Initial release

0 commit comments

Comments
 (0)