• Resolved ShMk

    (@shmk)


    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/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    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

    (@shmk)

    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

    (@takayukister)

    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

    (@shmk)

    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.