Skip to content
Home » Documentation » Contact Form 7 – Dynamic Text Extension » Shortcodes » DTX Shortcode: Media Attachment

DTX Shortcode: Media Attachment

This shortcode returns the absolute URL of the current post or page’s featured image.

[CF7_get_attachment]

Form Tag Example

The dynamic text form tag when used inside the form editor of Contact Form 7 would look like this:

[dynamictext inputname "CF7_get_attachment"]

Optional Shortcode Attributes

Absolute URL of the Current Post/Page’s Featured Image

If I wanted to prepopulate a dynamic field with the absolute URL of the current post or page’s featured image, the value of my dynamic field would need no parameters, just the shortcode tag.

CF7_get_attachment

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

[dynamictext input_name "CF7_get_attachment"]

Return the Attachment ID instead of the Absolute URL

By default, this shortcode returns the absolute URL, but if I wanted to return the attachment ID instead, the value of my dynamic field would only need to set the return attribute to id:

CF7_get_attachment return='id'

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

[dynamictext input_name "CF7_get_attachment return='id'"]

Get Featured Image from Another Post/Page

If I wanted to prepopulate a dynamic field with the URL or ID of the different post or page’s featured image, the value of my dynamic field would specify that post/page’s ID in a post_id attribute like this:

CF7_get_attachment post_id='123'

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

[dynamictext input_name "CF7_get_attachment post_id='123'"]

Get Any Attachment from Media Library

Of course this shortcode isn’t limited to just featured images, you can get the URL of any attachment from the media library by it’s attachment ID set as the value of the id attribute like so:

CF7_get_attachment id='123'

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

[dynamictext input_name "CF7_get_attachment id='123'"]

Specify Size

When an image is uploaded to the WordPress media library, WordPress core will attempt to create additional sizes: thumbnail, medium, and large. Your theme or plugins may have even more sizes defined.

By default, this shortcode returns the full size, which is to say the original size. If you want to return a specific size, simply set it in the size attribute like this.

CF7_get_attachment size='thumbnail'

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

[dynamictext input_name "CF7_get_attachment size='thumbnail'"]

Advanced Form Tag Example

Most of the optional shortcode attributes can be used at the same time. For example, if I wanted to retrieve the attachment ID of a featured image for a different post of the thumbnail size, then the dynamic text form tag would look like this:

[dynamictext input_name "CF7_get_attachment post_id='123' return='id' size='thumbnail'"]

The only two attributes that can’t play together is id and post_id. If both are specified, it will get the attachment specified by id and completely ignore the post_id attribute. If neither are specified, then it looks to the current featured image assigned to the global $post object.

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.

View Source Code

This shortcode was added to the Contact Form 7 – Dynamic Text Extension WordPress plugin in version 3.1.0. View the source code for this shortcode included with DTX.

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) ""
}