Skip to content
Home » Documentation » Contact Form 7 – Dynamic Text Extension » Shortcodes » DTX Shortcode: Post/Page Variables

DTX Shortcode: Post/Page Variables

This shortcode returns the value of a specified key that is found in the current global $post object. This shortcode includes a key attribute where you specify which variable you want to output.

[CF7_get_post_var key="<insert variable key>"]

This shortcode also includes an optional post_id attribute where you can specify the post ID for a different post or page to pull a variable from it instead.

[CF7_get_post_var key="<insert variable key>" post_id="<insert post ID>"]

Note: this shortcode only works on WordPress pages that are of the WP_Post type. If you’re looking to create a form that goes into templated area like the footer and you want fields for titles and URLs to work for archive pages as well, please see CF7_get_current_var instead!

Example

If I wanted to get the title of the current post, the value of my dynamic field would use this:

CF7_get_post_var key='post_title'

And then the dynamic text form tag would look like this:

[dynamictext inputname "CF7_get_post_var key='post_title'"]

I could also choose to display the post title of a different post or page than the one currently being viewed by using the following dynamic form tag:

[dynamictext inputname "CF7_get_post_var key='post_title'" post_id="123"]

Possible Options

Any property that is available from global $post object (type: WP_Post) and returns a string or numeric value can be used such as:

  • Post ID: CF7_get_post_var key='ID'
  • Title: CF7_get_post_var key='post_title'
  • Author: CF7_get_post_var key='post_author'
  • Publish date: CF7_get_post_var key='post_date'
  • Excerpt: CF7_get_post_var key='post_excerpt'

Cache Compatibility

If cache compatibility mode is enabled for the form tag that uses this built-in shortcode, it does not make an AJAX request. Instead, it simply uses the preexisting value because the value retrieved by this shortcode is safe to cache.

Demo

Check out these fields in action! I’m pulling in various information about this post including the publish date, last modified date, and excerpt!

    I made the custom post type docs for my website!

    View Source Code

    View the source code for the server-side shortcode or for the client-side JavaScript function.

    Related Post Module Attributes Before

    array(29) {
      ["post_type"]=>
      string(4) "post"
      ["post_id"]=>
      string(1) "0"
      ["exclude"]=>
      string(1) "0"
      ["title"]=>
      string(32) "Related Articles & Tutorials"
      ["description"]=>
      string(0) ""
      ["max"]=>
      string(1) "3"
      ["post_ids"]=>
      string(0) ""
      ["exclude_ids"]=>
      string(0) ""
      ["is_series"]=>
      string(0) ""
      ["featured_term"]=>
      string(3) "307"
      ["exclude_terms"]=>
      string(0) ""
      ["exclusive"]=>
      string(1) "1"
      ["order"]=>
      string(4) "DESC"
      ["show_image"]=>
      string(2) "on"
      ["image_size"]=>
      string(6) "medium"
      ["menu_order_label"]=>
      string(0) ""
      ["show_order_label"]=>
      string(2) "on"
      ["show_date"]=>
      string(2) "on"
      ["show_meta_keys"]=>
      string(2) "on"
      ["show_modified"]=>
      string(0) ""
      ["show_author"]=>
      string(0) ""
      ["show_categories"]=>
      string(0) ""
      ["show_primary_category"]=>
      string(0) ""
      ["show_description"]=>
      string(0) ""
      ["show_reading_time"]=>
      string(2) "on"
      ["show_cta"]=>
      string(2) "on"
      ["cta"]=>
      string(9) "Read more"
      ["autoplay"]=>
      string(0) ""
      ["allow_sticky"]=>
      string(0) ""
    }
    

    Related Post Module Attributes

    array(29) {
      ["post_type"]=>
      string(4) "post"
      ["post_id"]=>
      string(1) "0"
      ["exclude"]=>
      string(1) "0"
      ["title"]=>
      string(32) "Related Articles & Tutorials"
      ["description"]=>
      string(0) ""
      ["max"]=>
      string(1) "3"
      ["post_ids"]=>
      string(0) ""
      ["exclude_ids"]=>
      string(0) ""
      ["is_series"]=>
      string(0) ""
      ["featured_term"]=>
      string(3) "307"
      ["exclude_terms"]=>
      string(0) ""
      ["exclusive"]=>
      string(1) "1"
      ["order"]=>
      string(4) "DESC"
      ["show_image"]=>
      string(2) "on"
      ["image_size"]=>
      string(6) "medium"
      ["menu_order_label"]=>
      string(0) ""
      ["show_order_label"]=>
      string(2) "on"
      ["show_date"]=>
      string(2) "on"
      ["show_meta_keys"]=>
      string(2) "on"
      ["show_modified"]=>
      string(0) ""
      ["show_author"]=>
      string(0) ""
      ["show_categories"]=>
      string(0) ""
      ["show_primary_category"]=>
      string(0) ""
      ["show_description"]=>
      string(0) ""
      ["show_reading_time"]=>
      string(2) "on"
      ["show_cta"]=>
      string(2) "on"
      ["cta"]=>
      string(9) "Read more"
      ["autoplay"]=>
      string(0) ""
      ["allow_sticky"]=>
      string(0) ""
    }
    

    2 thoughts on “DTX Shortcode: Post/Page Variables”