Skip to content
Home » Documentation » Contact Form 7 – Dynamic Text Extension » Shortcodes » DTX Shortcode: Post Meta & Custom Fields

DTX Shortcode: Post Meta & Custom Fields

    This shortcode returns a meta value from the current post or page. This shortcode includes a key attribute where you specify the meta key or custom field’s ID.

    [CF7_get_custom_field key="<meta key>"]

    Example

    If I have the Yoast SEO WordPress plugin installed and activated on my website and wanted to prepopulate a form field with the current post’s meta description, I’d have to look at the meta value of “_yoast_wpseo_metadesc”. The value of my dynamic field would use this:

    CF7_get_custom_field key='_yoast_wpseo_metadesc'

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

    [dynamic_text inputname "CF7_get_custom_field key='_yoast_wpseo_metadesc'"]

    Possible Options

    Any property that is available in the post’s meta data that returns a string or numeric value can be used. If you don’t know the meta key, you can use the following code to display all meta data for the current post/page to take a peek:

    global $post;
    var_dump(get_metadata('post', $post->ID));

    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 it out! The form below has two fields, one using [dynamictext dynamictext-1 "CF7_get_post_var key='title'"] to get the current post’s title, and the second one using [dynamictext dynamictext-2 "CF7_get_custom_field key='_yoast_wpseo_title'"] to get the meta title I’ve set for this article using Yoast SEO.

      With Yoast SEO specifically, you’ll see my variables in the page title as well, so it might be better to create a custom shortcode that also runs the value of it through wpseo_replace_vars($string, $post).

      View Source Code

      View the source code for this shortcode included with DTX.

      Contact Form 7 Native Functionality

      In a later version of Contact Form 7, the default:{source} option was introduced for form tags. This native feature in Contact Form 7 may make it seem like our DTX shortcode is redundant, but not quite. In order for their method to work, the name of your form tag needs to match that of the key from your source. Using the same example from above, the form tag would look like this:

      [text _yoast_wpseo_metadesc default:post_meta]

      While we’re glad to see this functionality make it into the core of Contact Form 7, DTX offers the flexibility for mapping keys to form fields with different names, adding security through obfuscating the value, and compatibility with caching plugins. Of course, we like our method better especially for pulling in meta data with strange or long meta keys.

      Post IDs

      string(0) ""
      

      Related by Terms

      array(2) {
        [0]=>
        int(22678)
        [1]=>
        int(12046)
      }