Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Contact form 7 custom shortcodes

Contact form 7 has some shortcodes, like [_date] to get todays date. But I want to display the date one week from now.

So I need to create a custom shortcode to Contact form 7 that takes say [next_week] and in the recived email the correct date is displayed.

Where and how do I create custom shortcodes to Contact form 7?

like image 927
halliewuud Avatar asked Nov 01 '12 14:11

halliewuud


People also ask

How do I change the shortcode in Contact Form 7?

Open the editor page for the contact form you want to add (Contact > Contact Forms). Each contact form has its own shortcode, such as [contact-form-7 id="1234" title="Contact form 1"] . Copy the shortcode and paste it into the content of the post.

How do I add a shortcode to a contact form in WordPress?

If you're using the Classic editor, in your post or page editor, go to the Add Form button and click it. A popup box will appear. Select the contact form you want to add and click the Add Form button. A contact form shortcode will be added to your page content.


3 Answers

This is a bit late to the response party, but I keep seeing this post when I want to add custom shortcodes to my forms and message body. I wanted to be able to insert shortcodes without registering them special in CF7 and often in the message body only (something CF7 doesn't seem to be able to do).

Here's how I finally did it:

// 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;
}
like image 186
DACrosby Avatar answered Oct 05 '22 09:10

DACrosby


Add the following to your functions.php

wpcf7_add_shortcode('custom_date', 'wpcf7_custom_date_shortcode_handler', true);

function wpcf7_custom_date_shortcode_handler($tag) {
    if (!is_array($tag)) return '';

    $name = $tag['name'];
    if (empty($name)) return '';

    $next_week = date('Y-m-d', time() + (60*60*24*7)); 
    $html = '<input type="hidden" name="' . $name . '" value="' . $next_week . '" />';
    return $html;
}

Now in the "Form" field in CF7 GUI type [custom_date next_week]

Now you can use [next_week] in the message body.

like image 30
halliewuud Avatar answered Oct 02 '22 09:10

halliewuud


There are two types of tags in CF7: form tags (the contact form itself) and mail tags (a email) — read more.


  1. Custom form tags:

    To add a custom form tag you can use wpcf7_add_form_tag() function on wpcf7_init action hook (Read more).

    The wpcf7_add_shortcode() function in the accepted answer is considered to be deprecated and replaced by this function.

  2. Custom mail tags:

    I didn't find any built-in functionality to add custom mail tags but I believe there are multiple possible workarounds here:

    1. Enable custom shortcodes (don't forget to create the shortcode handler beforehand):

      • For mail components, according to the DACrosby's answer.
      • For entire mail (all components), using wpcf7_special_mail_tags filter:

    function my_special_mail_tag( $output, $name, $html ) {
        if ( 'myshortcode' === $name ) {
            $output = do_shortcode( "[$name]" );
        }
        return $output;
    }
    add_filter( 'wpcf7_special_mail_tags', 'my_special_mail_tag', 10, 3 );
    
    1. Add a custom hidden form tag with the prepopulated data to the form, and then use it in the mail: [my-custom-form-tag-with-some-prepopulated-data]; the custom form tag should be registered with the wpcf7_add_form_tag(), as specified above.
like image 28
vladlu Avatar answered Oct 03 '22 09:10

vladlu