Support » Plugin: Contact Form 7 » Fix for special mail tags when using form with php do_shortcode

  • Resolved VilaRestal


    I couldn’t find anyone else encountering this problem (although some threads about emails not working with special mail tags) but maybe people are and not posting about it or maybe someone will in future.

    For Contact Form 7, with the shortcode for the form put in the content of a page or post, the special mail tags that can be used in the form’s fields that refer to properties of the post, such as [_post_author_email] (perhaps as the address to send the notification to), works fine. A very useful feature.

    However, if you try to do the same thing php (as opposed to the page contents: perhaps you want to ensure that posts of a particular category always have a particular contact form on them that allow the visitor to email the author of the post) it doesn’t work but I’ve discovered how to make it work:

    For example, if the shortcode for a form is:

    [contact-form-7 id="123" title="Peculair Post Enquiry"]

    Then the php to use that would be:

    <?php echo do_shortcode('[contact-form-7 id="123" title="Peculair Post Enquiry"]'); ?>

    But the results will be slightly different.

    For the shortcode used directly in the posts content, the resultant html (view source of the page it produces), will include the following string in multiple places:


    f123 is the ID of the form’s template (id=”123″ above)
    p7890 is the ID of the post (a post with ID 2468 will have p2468)

    If you do the same in the the php version (echo do_shortcode) the p7890- is missing.

    In php we can get the ID of the post so the fix is to do a find/replace in the results of do_shortcode before echoing it like this:

    <?php echo str_replace('wpcf7-f123-o1', 'wpcf7-f123-p' . get_the_ID() . '-o1', do_shortcode('[contact-form-7 id="123" title="Peculair Post Enquiry"]')); ?>

    Of course, replace 123 (all three instances) with the ID of the form.

    Special mail tags that refer to the properties of the post (such as [_post_author_email]) will then work within the form.

    Whether that’s a bug with Contact Form’s 7 shortcode or PHP’s do_shortcode, I have no idea but there it is and the fix is simple enough.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Wow. thanks a lot..

    Works perfectly. Special mail tags not working in the php do_shortcode and the sidebar widget is quite annoying.

    Thanks for sharing the fix.


    You’re a rock star @vilarestal, thanks for this. My forms are working properly now.

    Hey there! I’ve encountered this problem as well, but didn’t like how undynamic the solution was (required knowledge of the id, so it wouldn’t work with the advanced custom fields addon for contact form 7), below is a short example of the code I did to make this more dynamic (though I’m sure it can be more optimised, but I needed a quick fix for this).

    $afterid = explode('id="wpcf7-f', $formshortcode);
    $formid = explode('-',$afterid[1],2);
    $form = str_replace(

    Where $formshortcode is either the shortcode of the form, OR, the output of the shortcode.

    Hey Guys,

    I agree with @lopeax that we needed a more dynamic solution so I wrote a function to replace the do_shortcode

    function fix_wpcf7_do_shortcode( $shortcode, $id ) {
        $regex_pattern = get_shortcode_regex();
        preg_match ('/'.$regex_pattern.'/s', $shortcode, $regex_matches);
        $shortcode_atts = shortcode_parse_atts($regex_matches[3]);
        $shortcode_id = $shortcode_atts['id'];
        return str_replace('wpcf7-f'.$shortcode_id.'-o1', 'wpcf7-f'.$shortcode_id.'-p' . $id . '-o1', do_shortcode($shortcode));

    You simply call it in place of do_shortcode in your template;
    <?php echo fix_wpcf7_do_shortcode($inquiries_form,get_the_ID()); ?>


    Hello Garret,

    Thanks for posting that. However there is a minor error. Through a little testing I found that the o1 at the end of the I’d actually differentiates forms if there are more than one of the same form on the same page, th is is why I left it out of my initial function. But it’s a minor thing as in almost every case the same form won’t be used more than once on a page.


    Thanks @lopeax,

    Good catch there, didn’t realize that was how the -o# portion worked, I’ve updated the function below as well as made it so you can provide a page id if you want it refering to another page or by not providing it defaults to the current page;

    function fix_wpcf7_do_shortcode( $shortcode, $id = NULL ) {
        if ( ! isset( $id ) )
            $id = get_the_ID();
        $regex_pattern = get_shortcode_regex();
        preg_match ('/'.$regex_pattern.'/s', $shortcode, $regex_matches);
        $shortcode_atts = shortcode_parse_atts($regex_matches[3]);
        $shortcode_id = $shortcode_atts['id'];
        return str_replace('wpcf7-f'.$shortcode_id.'-o', 'wpcf7-f'.$shortcode_id.'-p'.$id.'-o', do_shortcode($shortcode));


    Hey Garrett,

    No problem, I like functions being as complete and dynamic as possible. Thank for updating it!


    No worries, I’m with you there. Best to provide the most re-usable solution. Cheers

    Thread Starter VilaRestal


    Thank you for your kind words @garrett Hyder @ Eclipse Creative but many more thanks for making a more dynamic and reusable fix for this.

    Kudos to you and @lopeax for getting the ball rolling.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Fix for special mail tags when using form with php do_shortcode’ is closed to new replies.