Reputation: 55
I'm using a contact form 7 for the contact forms. I have a page for a doctor for example, and it has it's own form on the same page. How can I display the title of that page into the input field or select field on the contact form 7?
I did some research for dynamic data on contact form 7 and I found this https://wordpress.org/plugins/contact-form-7-dynamic-text-extension/
I put the following code into the form but it's not retrieving the page title:
[dynamictext dynamicname “CF7_get_post_var key=’title'”]
Any help is very much appreciated.
Upvotes: 1
Views: 13725
Reputation: 21
a year late, but I just saw it. I suppose you have already solved it. The error is because the shortcode has wrong characters. The correct thing is like this:
[dynamictext dynamicname "CF7_get_post_var key='title'"]
Regards!
Upvotes: 2
Reputation: 21
To use shortcode inside the wpcf7 forms, an easy way is "Berenis" solution, but with an important change.
Instead of add_shortcode( 'cf7_extra_fields', 'cf7_extra_fields_func' );
should be wpcf7_add_shortcode( 'cf7_extra_fields', 'cf7_extra_fields_func', true );
.
Remaining code can be same, on functions.php:
wpcf7_add_shortcode( 'cf7_extra_fields', 'cf7_extra_fields_func', true );
function cf7_extra_fields_func( $atts ) {
$html = '';
$html .= '<input type="hidden" name="page-title" value="'.get_the_title().'" />';
$html .= '<input type="hidden" name="page-url" value="'.get_the_permalink().'" />';
return $html;
}
On contact form, use: [cf7_extra_fields], on email fields use: [page-title] and [page-url].
This worked for me, without any issues.
Upvotes: 2
Reputation: 637
Put this to functions.php
add_shortcode( 'cf7_extra_fields', 'cf7_extra_fields_func' );
function cf7_extra_fields_func( $atts ) {
$html = '';
$html .= '<input type="hidden" name="page-title" value="<'.get_the_title().'">';
$html .= '<input type="hidden" name="page-url" value="<'.get_the_permalink().'">';
return $html;
}
Then when editing contact form add this shortcode inside [cf7_extra_fields]
When passing form fields to email use [page-title]
and [page-url]
No plugin needed
Upvotes: 1
Reputation: 1
Did you try with jquery/js ? like jQuery("#field_id").val("<?php echo get_the_title(); ?>");
. you can try this code in the page where the form is using.
Upvotes: 0