0

I have added the short-code in my Contact Form 7 Message Body but it is not working.

function.php

function wpb_demo_shortcode() { 

 $output = '<p style="font-size:11pt;font-family:Calibri,sans-serif;margin:0;">Lets stay connected!</p>';

 return $output;
 } 
 // register shortcode
 add_shortcode('greeting', 'wpb_demo_shortcode');

/**
 * A tag to be used in "Mail" section so the user receives the special tag
 * [greeting]
 */
add_filter('wpcf7_special_mail_tags', 'wpcf7_tag_tournament', 10, 3);
function wpcf7_tag_tournament($output, $name, $html)
{
    $name = preg_replace('/^wpcf7\./', '_', $name); // for back-compat

    $submission = WPCF7_Submission::get_instance();

    if (! $submission) {
        return $output;
    }

    if ('greeting' == $name) {
        return $submission->get_posted_data("greeting");
    }

    return $output;
}

// Other Code 
add_filter( 'wpcf7_form_elements', 'mycustom_wpcf7_form_elements' );

function mycustom_wpcf7_form_elements( $form ) {
$form = do_shortcode( $form );

return $form;
} 

I have added this code in my functions.php but my shortcode is not showing html in the email.

Added this in the Message Body: [greeting]

Any help is much appreciated.

2
  • Shortcodes are only parsed in post content. You could try echo do_shortcode('[greeting]') to try to force WP to parse it. – WebElaine Feb 26 '20 at 15:17
  • @WebElaine. In the message body or in the functions.php? – Rahul Kumar Feb 26 '20 at 18:03
1

You can place only the following code in functions.php and it will work:

// Activate Shortcode Execution for Contact Form 7

add_filter( 'wpcf7_form_elements', 'do_shortcode' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.