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

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.

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.

Using do_shortcode() with contact form 7 and form tag is missing

I’m trying to use the contact form in my template using do_shortcode and popup (Request availability button on a page). But I’ve noticed that form generated only inputs without <form> tag as usual so it failed to submit.
That’s how I’m implementing it in functions.php
function wc_shop_popup() {
$reqform = '[contact-form-7 id="987" title="Request availability"]';
echo '<a class="button button_full_width button_left req_button popup-link" href="#popup-availability" rel="lightbox" data-type="inline"><span class="button_icon"><i class="icon-layout"></i></span><span class="button_label">Request Availability</span></a>
<div id="popup-availability" class="popup-content mfp-hide"><div class="popup-inner" style="padding:20px;">'.do_shortcode($reqform).'</div></div>';
}
add_action( 'woocommerce_after_add_to_cart_button', 'wc_shop_popup' );
This is the page - Request availability button. Any suggestions?
The problem was that contact form has been generated inside of add to cart form. I used another woocommerce hook called "woocommerce_after_single_product_summary" instead of "woocommerce_after_add_to_cart_button" and it's working!

Wordpress / WooCommerce - dynamic hyperlink based on current product

I'm running WooCommerce in Wordpress here and on some products, I will be embedding a block of text which includes a hyperlink to a contact form. I would like the Subject on the contact form to populate with the name of the product where the hyperlink was clicked
I have established that I can populate a CF7 field by appending a query string to the URL (e.g. www...co.uk/contact-form/?subject=blah,blah), so if there was some method of dynamically altering the hyperlink in the aforementioned block of text when the product page is loaded so as to append the product name after the query string
Maybe to describe this a little clearer...
The block of text on the product page will look like this:
<a href='...co.uk/contact-form/?subject={NEED-TO-DYNAMICALLY-APPEND-PRODUCT-NAME-HERE}>Contact us...</a>
Any ideas folks of how I might approach this or has anyone tried something similar?
Thanks
So you want the title of product name ?
$product = wc_get_product( id );
$product_name = $product->get_title();
<a href='...co.uk/contact-form/?subject=<?php echo $product_name; ?>'>Contact us...</a>
If you want the title of the page you can give this parameter to your href
$pageTitle = get_the_title();

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

Add page id in html input on every page

I've written a WordPress plugin, which enables the admin to generate some html-code into the WYSIWYG editor. There are buttons in this html code which have a particular action - this is done via jQuery on the single page.
However, I need the page id as an input for jQuery; I want to add a hidden input field with the page id to every page.
Is there a hook which adds a particular html code to every page, so that i can access it via jQuery?
Solved:
function add_to_content($content) {
$content .= "<input type='hidden' name='post_id' value='".get_the_ID()."' />";
return $content;
}
add_filter('the_content', 'add_to_content');

Resources