Title: Wrap variable into brackets
Last modified: August 21, 2016

---

# Wrap variable into brackets

 *  Resolved [ShMk](https://wordpress.org/support/users/shmk/)
 * (@shmk)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/wrap-variable-into-brackets/)
 * Hello,
    I’m using CF7 3.8 and CF7 Dynamic Text 1.1.0.2 and if I try to wrap a
   variable into square brackets the placeholder is not substituted with the value:
 * `[ [username] ] [subject]`
    (without the spaces between brackets… WP Forum won’t
   let me insert it otherwise)
 * returns me
 * `[username] This is the subject`
 * instead of
 * `[Name of the user] This is the subject`
 * How can I fix it?
 * [https://wordpress.org/plugins/contact-form-7/](https://wordpress.org/plugins/contact-form-7/)

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/wrap-variable-into-brackets/#post-4860693)
 * Yes, Contact Form 7 treats `[[``fieldname``]]` that way, for avoiding replacing
   a tag to a value.
 * Can’t you use <> or {} instead?
 *  Thread Starter [ShMk](https://wordpress.org/support/users/shmk/)
 * (@shmk)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/wrap-variable-into-brackets/#post-4860694)
 * Thanks for the fast reply!
 * The problem is that the email has to be read by a 3rd party automated system 
   and that system is looking for [username] in the email subject
 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/wrap-variable-into-brackets/#post-4860719)
 * I see. Then, how about replacing {} to [] after Contact Form 7 processed {[username]}?
 * Add this snippet into your theme’s functions.php or somewhere convenient:
 *     ```
       add_filter( 'wpcf7_mail_components', 'your_wpcf7_mail_components' );
   
       function your_wpcf7_mail_components( $components ) {
       	$components['subject'] = preg_replace( '/{(.+)}/', "[$1]", $components['subject'] );
   
       	return $components;
       }
       ```
   
 * Obviously, you have to put {[username]} in your mail subject.
 *  Thread Starter [ShMk](https://wordpress.org/support/users/shmk/)
 * (@shmk)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/wrap-variable-into-brackets/#post-4860749)
 * I thought on using a replace before sending the email but I didn’t know what 
   hook or filters use.
    I had also to execute a strtoupper on that variable so 
   I replaced the preg_replace with a preg_replace_callback and it worked as needed
   🙂
 * Thanks a lot! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Wrap variable into brackets’ is closed to new replies.

 * ![](https://ps.w.org/contact-form-7/assets/icon.svg?rev=2339255)
 * [Contact Form 7](https://wordpress.org/plugins/contact-form-7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-7/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-7/reviews/)

## Tags

 * [brackets](https://wordpress.org/support/topic-tag/brackets/)
 * [substitution](https://wordpress.org/support/topic-tag/substitution/)

 * 4 replies
 * 2 participants
 * Last reply from: [ShMk](https://wordpress.org/support/users/shmk/)
 * Last activity: [12 years, 1 month ago](https://wordpress.org/support/topic/wrap-variable-into-brackets/#post-4860749)
 * Status: resolved