How to make mobile number validation in contact form 7? - wordpress

How to create custom mobile number validation in Wordpress contact form 7 plugin is it possible please help me.
Actually i want to only valid mobile number is allowed this field not taken any other character for ex- 9708644571

To use the phone number validation , you need to put the number field as per example given below.
[number* your-number min:9 max:10 step:3 class:required "40"]
Reference link : CF7

If you want to use custom validation for mobile number, then you can use jQuery also.
Just like this:
$('.wpcf7-form .custom-field-class input').on('keypress', function(e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 43 || e.which > 57)) {
return false;
}
});
Place this file in your page template where form exists or in the footer.php or in your custom js file.
Please replace the custom-field-class with your input field wrapper class or you can modify your classes & inputs name according to your requirement.
Hope, this may be helpful to you.

// Phone number validation : format and 10 digits
// Add code in your functions.php file
function custom_contact_validation_filter( $result, $tag ) {
// 'contact': is name of tag in contact form
if ( 'contact' == $tag->name ) {
$re_format = '/^[0-9]{10}+$/'; //9425786311
$your_contact = $_POST['contact'];
if (!preg_match($re_format, $your_contact , $matches)) {
$result->invalidate($tag, "Enter 10 digits only" );
}
}
return $result;
}
add_filter( 'wpcf7_validate_text*', 'custom_contact_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_text', 'custom_contact_validation_filter', 10, 2 );

Related

Gravity Forms - Accessing field defined in shortcode

I'm writing a rating system using Gravity Forms. Next to each post is a "Vote" link which opens a lightbox with a Gravity Forms shortcode in it. The shortcode populates two hidden fields - the Post ID and the User ID:
[gravityform id="2" field_values="gf_rating_post=123&gf_rating_user=456" title="false" description="false" ajax="true" tabindex="49"]
Inside the form are dropdown menus to rate the various elements, from 1-10. I'm trying to write code to insert SELECTED with the previously selected value from the dropdown, but I need to be able to reference the Post ID in order to retrieve that previously selected value.
add_filter('gform_pre_render_2', 'ac_populate_post_ratings');
add_filter('gform_pre_validation_2', 'ac_populate_post_ratings');
add_filter('gform_pre_submission_filter_2', 'ac_populate_post_ratings');
add_filter('gform_admin_pre_render_2', 'ac_populate_post_ratings');
function ac_populate_post_ratings($form) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != "select" || strpos( $field->cssClass, 'populate-posts' ) ) {
continue;
}
$choices = $field->choices;
foreach ( $choices as $choice ) {
// echo "defaultValue: " . $field->defaultValue . "<br />";
$newchoices[] = array( 'text' => $choice['text'], 'value' => $choice['value'] );
}
$field->choices = $newchoices;
}
return $form;
}
It seems that because the Post ID value is dynamically added to the form, it isn't able to be referenced using the field's defaultValue, which is always empty even though when you view the source, the input value is set correctly.
Any ideas how I can reference that Post ID inside the ac_populate_post_ratings() function?
As a last ditch effect, I contacted Gravity Forms support, and they came through with the goods.
The field defaultValue property will only contain a value if you have configured that setting in the form editor. Values which are being dynamically populated are not available from the $form.
The gform_pre_render filter is passed the dynamic population values from the shortcode in its third param.
I needed to change this line:
add_filter('gform_pre_render_2', 'ac_populate_post_ratings');`
to this:
add_filter('gform_pre_render_2', 'ac_populate_post_ratings', 10, 3 );`
And this line:
function ac_populate_post_ratings($form) {
to this:
function ac_populate_post_ratings( $form, $ajax = false, $field_values = array() ) {
And now I can access the values like so:
$gf_rating_post = rgar( $field_values, 'gf_rating_post' );

WordPress Shortcode with fallback

I'm building a simple plugin that uses the geoservices web service and what I'm trying to do is dynamically change the content on a WordPress page based on their location. I have it working somewhat but my issue is that it's returning both the location-specific text AND the default. I know it's because i'm using the shortcode instance more than once but I don't know how to change it to ONLY show the location specific content and if the location is not set or does not match the shortcode params then fall back to the default one. I don't want to add "default" as a shortcode param because it could contain HTML or something else.
Here is an example of my shortcode:
[geo city="Orlando"]555-123-6349[/geo][geo city="Raleigh"]919-999-9999[/geo][geo city="Default"]Default text here[/geo]
So based on the above, the desired result would show Orlando's phone number if the user is from Orlando or it would show Raleigh number if they are from Raleigh. Otherwise, if they are not from either of those places, it would use the default.
Here is my shortcode:
function geo_services( $atts , $content = null ) {
// Attributes
extract(shortcode_atts(array(
'city' => '',
'state' => '',
), $atts));
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
if($city === $geoplugin->city){
return $content;
} elseif ($state === $geoplugin->region){
return $content;
} elseif ($city === 'Default') {
return $content;
}
}
add_shortcode( 'geo', 'geo_services' );
And here is what is happening when I use the example shortcode above:
I believe you may be misunderstanding how shortcodes work in WP. In your example, you have added 3 shortcodes to the content. Each of those shortcodes is going to run. Not one or the other. So doing,
[geo city="Orlando"]555-123-6349[/geo][geo city="Raleigh"]919-999-9999[/geo][geo city="Default"]Default text here[/geo]
means that each of those will be called and evaluated. $geoplugin->city is always going to return the city of the user, regardless of what attributes you supplied. And since you are returning $content in all cases, it will always spit out the content that you added inside the shortcode. This is why you are seeing all 3 responses.
Instead, I would try the approach below. If your goal is to spit out content based on the city of the user, you really don't need to supply an attribute to the shortcode. See the following example:
//in your post/page content, simply use the shortcode geo
[geo]
//your function should be
function geo_services( $atts , $content = null ) {
//
require_once('geoplugin.class.php');
//
$geoplugin = new geoPlugin();
$geoplugin->locate();
//
switch( $geoplugin->city ) {
case 'Orlando':
return '555-123-6349';
break;
case 'Raleigh':
return '919-999-9999';
break;
default:
return 'Default text here';
break;
}
}
add_shortcode( 'geo', 'geo_services' );
Providing another answer based on OP comments. If you really need to manage the content via the WYSIWYG, then you could supply the content for each city as an attribute.
//add shortcode to post/page content
[geo orlando="555-123-6349" raleigh="919-999-9999" default="Custom default text here"]
//your function should be
function geo_services( $atts , $content = null ) {
//don't use extract since we expect X number of atts now
$atts = shortcode_atts(array(
'default' => 'Default text here'
), $atts);
//
require_once('geoplugin.class.php');
//
$geoplugin = new geoPlugin();
$geoplugin->locate();
//was the city provided as an attribute?
if( isset($atts[ strtolower($geoplugin->city) ]) ) {
return $atts[ strtolower($geoplugin->city) ];
}else {
return $atts['default'];
}
}
add_shortcode( 'geo', 'geo_services' );
You may have to get creative with the HTML portion of the content, but now you can include X number of cities, with their custom content, in the shortcode itself. If the city is not supplied, or does not match, it will fallback to the default.

Contact Form 7: Reset only certain fields?

I'm using Wordpress (5.2.2) and Contact Form 7. How can I reset only certain fields on submit?
The default action on successful submit is to clear all of the form fields. I located what appears to be the call to reset in public_html/wp-content/plugins/contact-form-7/includes/js/scripts.js and commented out the indicated three lines,
if ( 'mail_sent' == data.status ) {
// $form.each( function() {
// this.reset();
// } );
wpcf7.toggleSubmit( $form );
}
which appears to now prevent resetting of all of the fields when the user clicks "send". However, I have a couple of pull-down selection fields ( [select id "opt1" "opt2" ... ] ) which I need to reset. Is there a way I can do this?
if ( 'mail_sent' == data.status ) {
$form.each( function() {
$.each( $(this)[0], function() {
if (!($(this).hasClass("noResetOnMailSent"))){
$(this).not(':button, :submit, :reset, :hidden').val('').prop('checked', false).prop('selected', false);
}
} );
} );
this issue already solved on official site of WordPress. check here
https://wordpress.org/support/topic/how-to-not-clear-or-autofill-certain-fields-after-submission/

Modify Contact Form 7 Submission Data

I'm using Contact Form 7 on a site and need to alter the checkbox submitted data. The checkbox has a label called 'Tick here if you dont want to receive further marketing', when this is checked the value sent in the admins notification email displays the checkbox label.
So it looks like:
Tick here if you dont want to receive further marketing: Tick here if you dont want to receive further marketing
I want to alter it so when its checked the value posted is No.
I believe I can use the following action hook to achieve this but I dont know how to check if the checkbox has been ticked within this function and modify its value.
Any help much appreciated.
// define the wpcf7_posted_data callback
function action_wpcf7_posted_data( $array ) {
// make action magic happen here...
};
// add the action
add_action( 'wpcf7_posted_data', 'action_wpcf7_posted_data', 10, 1 );
I believe you can just use:
// define the wpcf7_posted_data callback
function action_wpcf7_posted_data( $array ) {
//'checkbox-name' is the name that you gave the field in the CF7 admin.
$value = $array['checkbox-name'];
if( !empty( $value ) ){
$array['checkbox-name'] = "New Value";
}
return $array;
};
add_filter( 'wpcf7_posted_data', 'action_wpcf7_posted_data', 10, 1 );

Get custom field value but hide first 19 and last 2 characters

Geeks and code Gods,
I am having an issue with my wordpress code. I have added 3 extra columns to the dashboard's pages screen.
One of which displays the custom field 'scode'. The scode custom field holds the cart66 shortcode which adds an 'add cart' button to the page it is assigned to.
The shortcode looks like this:
[add_to_cart item="HD-NL-7010"]
Each page has a different shortcode.
Every shortcode is the same length, 31 characters long.
However Id prefer not to show the whole shortcode in the column, only the product code *HD-NL-7010* section of the code.
Is it possible to hide the first 19 and last 2 characters. Basically removing [add_to_cart item=" and "]
Below is the code I added to function.php to add the custom columns to the pages table:
function test_modify_post_table( $column ) {
$column['scode'] = 'Product Code';
$column['Price'] = 'Price';
$column['Product_Image'] = 'Image';
return $column;
}
add_filter( 'manage_pages_columns', 'test_modify_post_table' );
function test_modify_post_table_row( $column_name, $post_id ) {
$custom_fields = get_post_custom( $post_id );
switch ($column_name) {
case 'scode' :
echo $custom_fields['scode'][0];
break;
case 'Price' :
echo '£' . $custom_fields['Price'][0];
break;
case 'Product_Image' :
echo $custom_fields['Product_Image'][0];
break;
default:
}
}
add_filter( 'manage_pages_custom_column', 'test_modify_post_table_row', 10, 2 );
Thank you for reading this post. Any help would be greatly appreciated.
P.S. I'm going to ask this in another post but does anyone know how to get the Product_Image to show as an image in the page column rather than the image attachment ID?
I might be misunderstanding, does PHP's substr help? Something like:
case 'scode' :
echo substr( $custom_fields['scode'][0], 19, 10 );
break;
For what it's worth, I think I'd store just the code (HD-NL-7010) in the custom field, rather than the shortcode code, then apply the shortcode where needed (with do_shortcode).

Resources