Contact form 7 _post title not working? [_post title] error?

This article was written by in December 5, 2014, & may not be posted on other sites!
Original source url for this article: Contact form 7 _post title not working? [_post title] error?

Contact form 7 _post title not working? [_post title] error?
Are you trying to use contact form 7 special tags [_post title] , but all you get in your recieved mail is the code “[_post title] ” and not the actual post title? This is a classic contact form 7 [_post title] error!

Contact form 7 _post title not working? [_post title] error?

 

PROBLEM: Contact form 7 _post title not working? [_post title] error?

Are you trying to use contact form 7 special tags [_post title] , but all you get in your recieved mail is the code “[_post title] ” and not the actual post title? Well, the special tags / shortcodes like [_post title] can only be used when the contact form 7 is put directly inside a post or a page inside WordPress admin. If you put the contact form 7 inside a widget, template or any other theme file, the tags / shortcodes like [_post title] will not work!

 

SOLUTION: when using Contact form 7 in templates or theme files

Are you displaying your contact form within a template or theme file using php code, something like this:
<?php  echo do_shortcode('[contact-form-7 404 "Not Found"]');  ?>
Unfortunately you can’t pass any php variables or values into this shortcode, like a dynamic subject, but you can alter values like the “subject” or any other field using jQuery.

 

This is how to add the post title as subject in contact form 7 using jQuery

You need to use jQuery to alter the subject / any other value in contact form 7, something like this:
$("input").val("My new subject");
To change the placeholder text, use this code:
$(".your-subject input").attr("placeholder", "My new placeholder text").val("").focus().blur();

 

Code Example: Dynamic post title for each post button form

This is how to dynamically change the static subject to any other dynamic value from inside your template file using jQuery.
In this example we have all the posts listed on one page, each post have an “order” button. The contact form 7 is displayed in a pop-up when user clicks “order”, and we want the subject to dynamically be the same value as the current post title. (only one contact form needed)

  1.  Go to your contact form 7 field settings inside wordpress admin.
  2. Add a subject field in the form section, something like this:
    <p>Subject<br />
    [text your-subject] </p>
  3. Echo the contact form 7 shortcode inside your theme file, something like this:
    <?php  echo do_shortcode('[contact-form-7 404 "Not Found"]');  ?>
  4. Assuming you are working the loop, put the title of the post as an attribute in any element, something like this:
    <a class="my-subject" dataTitle="<?php the_title(); ?>">Order</a>
  5. In Jquery: On click, save the current attribute “dataTitle” (containing the title of the current post). Put this text as the subject in your contact form as soon as someone clicks “order”. Jquery code looks something like this:
    $('.my-subject').click(function(){
    var title = $(this).attr('dataTitle'); //put the dataTitle in the variable title
    $(".your-subject input").val( title); //put the title as the subject in your contact form 7
    });

 

Contact form 7 _post title only works when contact form is put inside a post or page.

Well, the special tags / shortcodes like [_post title] can only be used when the contact form 7 is put inside a post or a page in WordPress. If your contact form 7 is put directly inside a post or page in wordpress, you can then use the special tags / short codes inside the form settings in WordPress admin to automate different values, for example set the subject to be the same as the post title.

Special tags / shortcode list:

[_remote_ip] This tag will be replaced by the sender’s client IP address.
[_url] This tag will be replaced by the URL of the contact form.
[_date] This tag will be replaced by the date of the submission.
[_time] This tag will be replaced by the time of the submission.
[_post_id] This tag will be replaced by the ID of the post which contains the contact form.
[_post_name] This tag will be replaced by the name (slug) of the post which contains the contact form.
[_post_title] This tag will be replaced by the title of the post which contains the contact form.
[_post_url] This tag will be replaced by the permalink of the post which contains the contact form.
[_post_author] This tag will be replaced by the author name of the post which contains the contact form.
[_post_author_email] This tag will be replaced by the author email of the post which contains the contact form.

 

 

Spread the word

Facebooktwittergoogle_pluslinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *