Display n characters of Text field from Contact Form 7 - wordpress

I'm not sure if this question was asked but I couldn't find the solution.
I want to display the first 2 characters from the Contact form 7 text field in the mail.
For example, a person types "TODAY" in the text field.
In the email, I would like to display on the first 2 characters which are "TO" in the mail section when the admin receive the email.
How can I achieve this?

You could do something like this
Add this script however you are adding scripts to your page, in a static .js file, or you can add this to the end of your contact form.
<script>
jQuery('input[name="your-name"]').blur(function () {
var s = jQuery(this).val().substr(0, 2);
if (jQuery('#name-value').length) {
jQuery('#name-value').val(s);
} else {
jQuery(this).after('<input name="name-value" id="name-value" type="hidden" value="' + s + '">');
}
});
</script>
Replace 'first-name' with whatever you are using here and replace 'name-value' with what you want to use for the email form.
Then in your admin email, use the form tag
[name-value] and it will show up in the email. Contact form 7 turns all form fields into tags by name.

Related

Change characters of checkout field in WooCommerce to UPPERCASE

How can I change the characters of the checkout field in WooCommerce to uppercase? I have changed it by CSS (text-transform) but it is changing letters to uppercase only in frontend. When customers write lowercase characters to a field, then it is sent to the system with lowercase. ID of field is "billing_company_wi_vat". I will be grateful for any suggestion.
I found on the internet something like this but I don't know how to use it.
var upper = text.toUpperCase();
function wc_checkout_alter_field_input() {
if (is_checkout()):
if (!wp_script_is('jquery', 'done')) {
wp_enqueue_script('jquery');
}
wp_add_inline_script('wc-checkout', 'jQuery(document).ready(function($){ $("#billing_company_wi_vat").keyup(function() { this.value = this.value.toUpperCase();});});');
endif;
}
add_action('wp_enqueue_scripts', 'wc_checkout_alter_field_input');
Add this to your active theme functions.php file. The function wp_add_inline_script allows to add extra code to a registered script. wc-checkout-js will be rendered in the checkout page by WooCommerce. so we can add some additional JS through that ID wc-checkout

Contact form 7 required one of 2 different fields

I have a image upload field and a URL field in CF7 and how can i make it that a user needs to use one of the 2 fields. So select a image or put a link in the URL field?
I already search a little and i saw some options to do it on normal txt fields but that was not working for me i think because of the upload field.
I don't think that CF7 can do this out of the box. You would probably have to go down to code to solve this issue.
Since I don't know how big you form is this could maybe help you out:
https://wordpress.org/plugins/cf7-conditional-fields/ - Plugin for conditional field inside CF 7
https://conditional-fields-cf7.bdwm.be/conditional-fields-for-contact-form-7-tutorial/ - Tutorial how to use it.
So with this plugin you could basically create a dropdown in which the user first has to select if he wants to use an image or use the URL field. After that, only his selection would pop up and he can proceed with the selected option.
If you want to do the coding you can add some JS to the site and check on validation if one of the two fields is filled out or not.
If the plugin solves your issue - great, if not let me know so I can help you out with the code validation.
###Edit since we need code###
So to dynamically alter the required function I found a pretty good post:
Dynamically Disable Contact Form 7 Field Validation
Some Background information:
CF7 does the validation on server side, so working with JS or JQuery won't help in this case. What we are going to do is manipulate the server validation. If one of the fields is filled out correctly we are just putting content in the other field on the validation.
I created a form that looks like this:
The form in CF7 module looks like this:
<label> URL
[url* url ]
<label> Image
[file* image ]
[submit "Send"]
Then you just have to add the following code to your functions.php file of your theme.
function alter_wpcf7_posted_data( $data ) {
if($_FILES['image']['size'] != 0) {
$_POST['url'] = "http://fileupload.com";
}
if($_POST['url'] != "") {
$_FILES['image']['tmp_name'] = "urlprovided";
}
return $data;
}
add_filter("wpcf7_posted_data", "alter_wpcf7_posted_data");
Normally both fields will be empty in the beginning, if one of them has contet the other one gets fake data and the use has only to provide one.
Let me know if this helps.
I think i found the solution with a little help of the code from #Aschi33.
I made the image (upload field) required and the url field not. So there needs to be alwasy a image selected. But with this code:
function alter_wpcf7_posted_data( $data ) {
if($_POST['url'] != "") {
$_FILES['image']['tmp_name'] = "urlprovided";
}
return $data;
}
add_filter("wpcf7_posted_data", "alter_wpcf7_posted_data");
if the URL field is filled in then the required image field is faked to be filled in so then the URL field can be used to submit the form.
And then with this code:
function cf7_custom_url_check( $result, $url ) {
if ($result) {
$regex='/^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/';
if (!preg_match($regex,$url)) $result=FALSE;
}
return $result;
}
add_filter( 'wpcf7_is_url', 'cf7_custom_url_check', 10, 2 );
I check if its a real video link from YouTube and if not the field will give a error.

How to get a custom value using Contact Form 7 - Dynamic Text Extension

I have a $_SESSION array variable with post ids. Inside foreach loop, I would like to get the posts titles of these ids. Thus so far I have something like this:
sport_title = '';
foreach($_SESSION['sports_post_id'] as $sports_id {
$sport_title = get_the_title($sport_id);
$sports_titles .= $sport_title . "<br />";
}
Now, my problem is that I do not know how to pass it in a custom variable in Contact Form 7 - Dynamic Text Extension plugin.
I have inside my form this field (inside CF7):
[dynamichidden dynamic_sports readonly default:shortcode_attr]
and inside my custom page template php file:
echo do_shortcode('[contact-form-7 id="3561" "CF7_get_custom_field dynamic_sports=\'$sports_titles\'" title="Availability Form EN"]');
Thus, I would like to send these post titles in email.. How can I make it work? thanks in advance
ok I figure it out how to do it! If anyone wants more explanation:
Inside Contact Form 7 - Form tab, I have insert this code:
[dynamichidden dynamic_sports "CF7_GET key='sports_post_id'"]
where key is a standard word (could not change it).
Inside Email tab, you should have this code:
Sports: [dynamic_sports]
Now, inside my Custom template PHP file, I have this shortcode:
echo do_shortcode('[contact-form-7 id="3561" title="Availability Form EN"]');
I also have a form with a hidden input type, with a name sports_post_id and value the id of the current post:
<input type="hidden" value="<?php echo get_the_title( get_the_ID() ); ?>" name="sports_post_id" id="sports_post_id" />
EDITED
Another solution via plugin that extends the CF7, would be the following:
Install Contact Form 7 - Dynamic Text Extension
Copy and paste the form-tag code below and then add it inside the form code block
[dynamichidden page-title "CF7_get_post_var key='title'"]
The above code will add a hidden text input to the form which will pre-populate the page title. This is good to use when you are using the same contact form on multiple pages so you know where the user has submitted the form from. Alternatively, you can display the page URL or slug using one of the below shortcodes instead:
[dynamichidden page-url "CF7_bloginfo show='url'"]
[dynamichidden page-slug "CF7_bloginfo show='url'"]
Displaying the Hidden Dynamic Content Tag Variable in Contact Form 7
Finally, display the hidden dynamic content tag variable in Contact Form 7 form. While you are on the CF7 settings page, click on the "Email" tab and insert this:
[page-title]
If you are using the URL or Slug fields, you these instead:
[page-url]
[page-slug]
In your CF7 Form configuration > Email Tab, you only have to add the desired field between hooks [...]
[dynamic_sports]
This will print the dynamic field value in your email.

Add Dynamic field in Contact form 7

I have a query related to Dynamic hidden field to be set in Contact form 7
I want to set current date as dynamic hidden field in contact form. Any Help.
My code is "[dynamichidden post_date "post_date"]"
I am using contact form7 and Contact Form 7 - Dynamic Text Extension
Thank you
You can create a shortcode which will return current date and add that shortcode in contact form as follows -
Create shortcode in functions.php file
function custom_post_date_callback(){
return current_time('mysql');
}
add_shortcode('custom_post_date', 'custom_post_date_callback');
In post or page you will need to add shortcode as follows -
[dynamichidden post_date "custom_post_date"]
I thing you to get current date you have to create shortcode...
put this function in functions.php
function displaydate(){
return date('F jS, Y');
}
add_shortcode('date', 'displaydate');
you can change date format as you wanted...
put this date shortcode without bracket in dynamic value field in dynamichidden input field..
check this image http://dev.savivatech.com/sa/wp-content/uploads/2017/07/current-date.png
[dynamichidden dynamichidden-693 "date"]
this dynamichidden-693 is dynamic name...
Hopes this will help you

How to display a field in user meta in default WordPress registration form

I have a requirement where I wish to show a field office_name in WordPress default registration form.
What I did so far: I have created a field called 'office_name' by using the following code:
function my_show_extra_profile_fields() {
$user_contact_method['office_name'] = 'name of your office';
$user_contact_method['office_location'] = 'location of your office';
return $user_contact_method;
}
add_filter( 'user_contactmethods', 'my_show_extra_profile_fields' );
The office_name field is appearing in the USERS tab in the back end and getting updated when I enter the values there.
I want it to appear in the registration form.
You need to customize your registration form . Here is the Wordpress codex link.
https://codex.wordpress.org/Customizing_the_Registration_Form

Resources