Add Dynamic field in Contact form 7 - wordpress

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

Related

Programatically add Form Title to all Contact Form 7 forms?

I'm using Contact Form 7 in WordPress and I have many forms.
I'd like to add the form title (the post_title field in database terms) to all my existing forms, as a hidden field.
I'd like to use a hook so that I don't need to use a shortcode in each form in the admin area.
Any ideas? Thanks.
you could use CF7's 'wpcf7_form_hidden_fields' filter before your form is displayed,
add_filter('wpcf7_form_hidden_fields', 'add_form_title');
function add_job_title($hidden){
$form = wpcf7_get_current_contact_form();
$post = get_post($form->id());
$hidden['cf7_title'] = $post->post_title; //form title slug.
return $hidden;
}
when the form is submitted, $_POST['cf7_title'] will have the value of the hidden field.

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.

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

Override WordPress [gallery] shortcode insertion with different name

Say you created your own Gallery plugin that uses it's own shortcode (eg: [my_gallery] )
How would you go about modifying the default WordPress gallery shortcode insertion within the editor of [gallery] to [my_gallery] so that the client is not confused with having to remember the custom shortcode and accidentally making incorrect gallery insertions?
first remove the gallery shortcode , and then add your own ...
remove_shortcode('gallery', 'gallery_shortcode'); // removes the original shortcode
add_shortcode('gallery', 'my_awesome_gallery_shortcode'); // add your own shortcode
function my_awesome_gallery_shortcode($attr) {
$output = 'Codex is your friend' ;
return $output;
}
or as an alternative just rename your shortcode to something less confusing :-)

Resources