monakh2
11/17/2019 - 3:39 PM

Allow custom shortcodes in CF7 HTML form

// Allow custom shortcodes in CF7 HTML form
add_filter( 'wpcf7_form_elements', 'dacrosby_do_shortcodes_wpcf7_form' );
function dacrosby_do_shortcodes_wpcf7_form( $form ) {
    $form = do_shortcode( $form );
    return $form;
}

// Allow custom shortcodes in CF7 mailed message body
add_filter( 'wpcf7_mail_components', 'dacrosby_do_shortcodes_wpcf7_mail_body', 10, 2 );
function dacrosby_do_shortcodes_wpcf7_mail_body( $components, $number ) {
    $components['body'] = do_shortcode( $components['body'] );
    return $components;
};

// Add shortcode normally as per WordPress API
add_shortcode('my_code', 'my_code_callback');
function my_code_callback($atts){
    extract(shortcode_atts(array(
        'foo' => 'bar'
    ), $atts));

    // do things
    return $foo;
}