Skip to content

Commit 4416140

Browse files
committed
Basic Feature for WpForm Field Controlling
1 parent 4ffc100 commit 4416140

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed

assets/Facebook.svg

+1
Loading

assets/Github.svg

+1
Loading

assets/Linkedin.svg

+1
Loading

assets/devrahman.png

21.8 KB
Loading

devrahman.png

21.8 KB
Loading

wpforms-forms-fields-handler.php

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
/*
3+
Plugin Name: WPForms Forms Field Handler
4+
Plugin URI: https://devrahman.com/
5+
Description: Plugin to set allowed, preferred, and blocked countries in WPForms phone number field, and optionally restrict input to numbers only.
6+
Author: Dev Rahman
7+
Version: 1.0
8+
Author URI: https://devrahman.com/
9+
*/
10+
11+
if ( ! defined( 'ABSPATH' ) ) {
12+
exit;
13+
}
14+
function wpffh_menu() {
15+
add_options_page(
16+
'WPForms Forms Field Handler Settings',
17+
'WPForms Forms Field Handler',
18+
'manage_options',
19+
'wpffh-forms-field-handler',
20+
'wpffh_settings_page'
21+
);
22+
}
23+
add_action( 'admin_menu', 'wpffh_menu' );
24+
25+
function wpffh_settings_page() {
26+
?>
27+
<div class="wrap">
28+
<h1>WPForms Forms Field Handler Settings</h1>
29+
<form method="post" action="options.php">
30+
<?php
31+
settings_fields( 'wpffh-forms-field-handler-group' );
32+
do_settings_sections( 'wpffh-forms-field-handler-group' );
33+
?>
34+
<table class="form-table">
35+
<tr valign="top">
36+
<th scope="row">Preferred Countries</th>
37+
<td>
38+
<input type="text" name="wpffh_preferred_countries" value="<?php echo esc_attr( get_option( 'wpffh_preferred_countries' ) ); ?>" />
39+
<p class="description">Comma-separated list of preferred countries (e.g., gb,us,de,in).</p>
40+
</td>
41+
</tr>
42+
<tr valign="top">
43+
<th scope="row">Allowed Countries</th>
44+
<td>
45+
<input type="text" name="wpffh_allowed_countries" value="<?php echo esc_attr( get_option( 'wpffh_allowed_countries' ) ); ?>" />
46+
<p class="description">Comma-separated list of allowed countries (e.g., gb,us,de,in).</p>
47+
</td>
48+
</tr>
49+
<tr valign="top">
50+
<th scope="row">Blocked Countries</th>
51+
<td>
52+
<input type="text" name="wpffh_blocked_countries" value="<?php echo esc_attr( get_option( 'wpffh_blocked_countries' ) ); ?>" />
53+
<p class="description">Comma-separated list of blocked countries (e.g., gb,us,de,in).</p>
54+
</td>
55+
</tr>
56+
<tr valign="top">
57+
<th scope="row">Restrict to Numbers Only</th>
58+
<td>
59+
<input type="checkbox" name="wpffh_restrict_numbers_only" value="1" <?php checked(1, get_option( 'wpffh_restrict_numbers_only', 0 ), true); ?> />
60+
<p class="description">Check this box to restrict the phone field to numbers only.</p>
61+
</td>
62+
</tr>
63+
</table>
64+
<?php submit_button(); ?>
65+
</form>
66+
<?php wpffh_display_branding(); ?>
67+
</div>
68+
<?php
69+
}
70+
71+
function wpffh_settings() {
72+
register_setting( 'wpffh-forms-field-handler-group', 'wpffh_preferred_countries' );
73+
register_setting( 'wpffh-forms-field-handler-group', 'wpffh_allowed_countries' );
74+
register_setting( 'wpffh-forms-field-handler-group', 'wpffh_blocked_countries' );
75+
register_setting( 'wpffh-forms-field-handler-group', 'wpffh_restrict_numbers_only' );
76+
}
77+
add_action( 'admin_init', 'wpffh_settings' );
78+
function wpffh_enqueue_script() {
79+
$preferred_countries = get_option( 'wpffh_preferred_countries', '' );
80+
$allowed_countries = get_option( 'wpffh_allowed_countries', '' );
81+
$blocked_countries = get_option( 'wpffh_blocked_countries', '' );
82+
$restrict_numbers_only = get_option( 'wpffh_restrict_numbers_only', 0 );
83+
?>
84+
<script type="text/javascript">
85+
jQuery(document).on('wpformsReady', function() {
86+
jQuery('.wpforms-smart-phone-field').each(function() {
87+
var $el = jQuery(this),
88+
iti = $el.data('plugin_intlTelInput'),
89+
options;
90+
91+
if (iti.d) {
92+
options = Object.assign({}, iti.d);
93+
} else if (iti.options) {
94+
options = Object.assign({}, iti.options);
95+
}
96+
97+
if (!options) {
98+
return;
99+
}
100+
101+
$el.intlTelInput('destroy');
102+
103+
<?php if ($preferred_countries) : ?>
104+
options.preferredCountries = ['<?php echo str_replace(',', "','", $preferred_countries); ?>'];
105+
<?php endif; ?>
106+
107+
<?php if ($allowed_countries) : ?>
108+
options.onlyCountries = ['<?php echo str_replace(',', "','", $allowed_countries); ?>'];
109+
<?php endif; ?>
110+
111+
<?php if ($blocked_countries) : ?>
112+
options.onlyCountries = options.onlyCountries ? options.onlyCountries.filter(country => !['<?php echo str_replace(',', "','", $blocked_countries); ?>'].includes(country)) : iti.getCountryData().map(country => country.iso2).filter(country => !['<?php echo str_replace(',', "','", $blocked_countries); ?>'].includes(country));
113+
<?php endif; ?>
114+
115+
$el.intlTelInput(options);
116+
117+
$el.siblings('input[type="hidden"]').attr('name', 'wpforms[fields][' + options.hiddenInput + ']');
118+
119+
<?php if ($restrict_numbers_only) : ?>
120+
$el.addClass('numbersOnly');
121+
<?php endif; ?>
122+
});
123+
124+
<?php if ($restrict_numbers_only) : ?>
125+
jQuery('.numbersOnly').keypress(function(e) {
126+
var charCode = (e.which) ? e.which : event.keyCode;
127+
if (String.fromCharCode(charCode).match(/[^0-9]/g)) {
128+
return false;
129+
}
130+
});
131+
<?php endif; ?>
132+
});
133+
</script>
134+
<?php
135+
}
136+
add_action( 'wpforms_wp_footer_end', 'wpffh_enqueue_script', 30 );
137+
138+
139+
function wpffh_display_branding() {
140+
$logo_url = plugin_dir_url(__FILE__) . 'assets/devrahman.png';
141+
$website_url = 'https://devrahman.com';
142+
$facebook_url = 'https://facebook.com/devrahmanbd';
143+
$github_url = 'https://github.com/devrahmanbd';
144+
$linkedin_url = 'https://linkedin.com/in/devrahmanbd';
145+
?>
146+
<div style="text-align: center; margin-top: 20px;">
147+
<img src="<?php echo esc_url($logo_url); ?>" alt="Dev Rahman Logo" style="max-height: 50px;"><br>
148+
<p>
149+
WPForms Forms Field Handler made with love by <a href="<?php echo esc_url($website_url); ?>" target="_blank">Dev Rahman</a>.<br>
150+
All rights reserved by <a href="<?php echo esc_url($website_url); ?>" target="_blank">DevRahman.com</a>.<br>
151+
<a href="<?php echo esc_url($facebook_url); ?>" target="_blank"><img src="<?php echo plugin_dir_url(__FILE__) . 'assets/Facebook.svg'; ?>" alt="Facebook" style="max-height: 20px;"></a> |
152+
<a href="<?php echo esc_url($github_url); ?>" target="_blank"><img src="<?php echo plugin_dir_url(__FILE__) . 'assets/Github.svg'; ?>" alt="GitHub" style="max-height: 20px;"></a> |
153+
<a href="<?php echo esc_url($linkedin_url); ?>" target="_blank"><img src="<?php echo plugin_dir_url(__FILE__) . 'assets/Linkedin.svg'; ?>" alt="LinkedIn" style="max-height: 20px;"></a>
154+
</p>
155+
</div>
156+
<?php
157+
}

0 commit comments

Comments
 (0)