WordPress / Web Development Tutorials
(Best WordPress Tutorials)

CSSHTMLJavaScriptjQueryMySQLPHPSilvaTechnologiesWooCommerceWordpress
Silva Web Designs - Blog

How to Dynamically Add a WordPress Post Title in Contact Form 7

The Contact Form 7 WordPress plugin is a very powerful plugin that is very typically used on a lot of WordPress websites that requires forms. There are times we run into situations where we need to dynamically add a WordPress post title, URL or fields in either the subject line or email body generated by the form. Luckily, this situation is very easy to handle with the help of an extension plugin. In this tutorial, we will teach you how to dynamically add a WordPress post title in the Contact Form 7 plugin.

Step 1: Install the Contact Form 7 Dynamic Text Extension Plugin

First things first, we need to download and install the ‘Contact Form 7 Dynamic Text Extension Plugin’ from the WordPress plugin directory. This plugin will allow the ability to add dynamic content capabilities to the ‘Contact Form 7 Plugin’.

Please Note:

  • – Make sure that you also have the ‘Contact Form 7 Plugin’ installed.
  • – This is a very powerful plugin and dynamically adding the WordPress post title is just one of its many capabilities.

Step 2: Generate the Hidden Dynamic Content Tag in Contact Form 7

Now we will generate the hidden dynamic content tag that will be used to dynamically add the WordPress post title in ‘Contact Form 7’.

While in the WordPress Dashboard, go to the ‘Contact Form 7’ settings page and make sure you are viewing the ‘Form’ tab. Copy and paste the form-tag code below 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'"]

Step 3: Displaying the Hidden Dynamic Content Tag Variable in Contact Form 7

Finally, we will display the hidden dynamic content tag variable in ‘Contact Form 7’ form.

While still on the ‘Contact Form 7’ settings page, click on the ‘Email’ tab. You can now use the variable code tag below to display the WordPress post title in either the ‘Subject’ or ‘Message Body’ of the ‘Contact Form 7’ form.


[page-title]

If you are using the URL or Slug fields in this example, you would use the below code tag:-


[page-url]
[page-slug]

If you are having problems getting this tutorial to work please reread the tutorial and try again, if you still cannot get it to work please leave us a comment below and we will respond as soon as possible. We hope this helps.

 

Nathan da Silva - Profile

Posted by: Nathan da Silva

Nathan is the Founder of Silva Web Designs. He is passionate about web development, website design and basically anything digital related. His main expertise is with WordPress, Magento, Shopify as well as many other frameworks. Whether you need responsive design, SEO, speed optimisation or anything else in the world of digital then get in touch. If you would like to work with Nathan, simply drop him an email at [email protected]

It’s good to share

21 thoughts on “How to Dynamically Add a WordPress Post Title in Contact Form 7

  1. Thank you so much. I spent a lot of time researching this problem and you are the first one to explain it in a way that actually works! Have a nice day.

  2. Is there a way for the user to actually see the the title of the post in the form they are completing? For example, the dynamic title would show at the top of the form that the user is completing.

    1. Are you referring to the form or the email?

      If you are referring to the form then you should use dynamictext instead of dynamichidden

  3. Hi, i have embed a contact form in a popup after clicking a button, inside a product, but i can’t populate either to post (product) title or the link.
    Is that possible?
    Thanks in advance.

    1. Yes, it is possible, you would have to add the popup within the WooCommerce template files, it’s best practice to create a /woocommerce/ folder in your Child Theme if you are making changes to the core files. If you need some help with this, I’m happy to help ( :

  4. Hi,
    I have a text box in my form where I want to display the page title.
    example Hello, I am interested in “then page title should show”
    How would I be able to do this?

    1. Okay, I had a little play around with it, you could achieve that by adding the following code to your functions.php file:

      
      wpcf7_add_shortcode( 'cf7_extra_fields', 'cf7_extra_fields_func', true );
      
      function cf7_extra_fields_func( $atts ) {
         $html = '';
         $html .= '<input type="text" name="page-title" value="Hello, I am interested in '.get_the_title().'" />';
         return $html;
      }
      

      This will create a shortcode you can use in CF7, then to display this shortcode you can add this within the form:

      [cf7_extra_fields]

  5. Hi Nathan please see below the code I am using.

    [textarea* Message id:Message class:Message placeholder “Hello, I am interested in then the Page title should be here” ]

    1. Sorry about the late reply, honestly, I’d say it is easier handling this in jQuery, to be honest. It can be done using a CF7 hook but this would work for you…

      
      var single_post_title = $('h3.entry-title').text();
      $("textarea.wpcf7-form-control.wpcf7-textarea").attr("placeholder","Hello, I am interested in then the"+single_post_title);
      

      Note, you will have to check the class of the Title of that page, so the element might need updating here…

      Secondly, the text replacement would affect all CF7 forms on the site, so you may want to add the ID of the form if you only want it to change on a particular form.

      Hope that helps buddy!

  6. Hi,

    Nathan,

    please see the error I get below.

    Your PHP code changes were rolled back due to an error on line 633 of file wp-content/themes/houzez/functions.php. Please fix and try saving again.

    syntax error, unexpected ‘var’ (T_VAR), expecting end of file

Join the discussion

Related Posts

Related - Our Top 10 Used Plugins For WordPress – 2021 Edition

Wordpress / 20th December 2020

Our Top 10 Used Plugins For WordPress – 2021 Edition

Read More Related - How to Prevent Multi-Submit on Contact Form 7

Wordpress / 7th October 2020

How to Prevent Multi-Submit on Contact Form 7

Read More