Programatically add Form Title to all Contact Form 7 forms? - wordpress

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.

Related

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.

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

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

Wordpress widget for post specific content in sidebar

I am looking for a wordpress plugin that will allow me to add a paragraph to the sidebar that is specific to the blog post. I would need to be able to add that text when creating the post. Is there something out there like that? I have been unsuccessful in searches.
Thanks
This can be easily solved using Custom Fields, the Text Widget and a Shortcode.
This bit of code goes in the theme's functions.php or, preferable, within a custom plugin.
1) Enable shortcodes for the Text Widget
add_filter( 'widget_text', 'do_shortcode' );
2) Define the shortcode, read comments for details
add_shortcode( 'mytext', 'so_13735174_custom_text_widget' );
function so_13735174_custom_text_widget( $atts, $content = null )
{
global $post;
// $post contains lots of info
// Using $post->ID many more can be retrieved
// Here, we are getting the Custom Field named "text"
$html = get_post_meta( $post->ID, 'text', true );
// Custom Field not set or empty, print nothing
if( !isset( $html ) || '' == $html )
return '';
// Print Custom Field
return $html;
}
3) Add a Text Widget in the desired sidebar.
Leave the title empty and put the Shortcode in the content: [mytext].
4) Now each page or post with a Custom Field named text will have its value printed in the Widget.
5) The $html can get fancy and multiple Custom Fields can be used.
This isn't something that I've ever personally done, but try this.
Summary: You will add the a paragraph using a custom field, then display it in a widget.
Details:
First, make sure custom fields are enabled. Edit a post, then click
the "screen options" at the top right of the page. If "Custom
Fields" isn't checked, check it. You should now see a custom field
area below the post editor.
Come up with a name for your custom field. Perhaps
"extra_paragraph". Now put that in the "name" field in the custom
field area.
Write your paragraph in the "value" field the custom field area.
Install the Custom Field Widget plugin, set it to display this
new "extra_paragraph" field. (widget appears to be untested with newer versions of Wordpress so cross your fingers!)
Now when you write or edit posts you should see this "extra_paragraph" field as an option in the "name" dropdown.

Resources