Wordpress Contact Form 7 Custom Shortcodes

Contact form 7 has short codes, for example [_date], to get today's date. But I want to show the date in a week.

So, I need to create a special code for the contact in form 7, which takes say [next_week], and the correct date is displayed in the returned message.

Where and how to create custom shortcodes in contact form 7?

+11
source share
5 answers

Add functions.php to your functions

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('Ym-d', time() + (60*60*24*7)); $html = '<input type="hidden" name="' . $name . '" value="' . $next_week . '" />'; return $html; } 

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

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

+17
source

This is a bit late for the return side, but I continue to see this post when I want to add my own short codes to my forms and body of the message. I wanted to be able to insert shortcodes without registering them in CF7 and often only in the message body (something like CF7 does not seem to be able to do).

Here is how I 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; } 
+1
source

CF7 has two types of tags: form tags (the contact form itself) and mail tags (email) - more .


  1. Custom form tags:

    To add a custom form tag, you can use the wpcf7_add_form_tag() function on the wpcf7_init action wpcf7_init ( Details ).

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

  2. Custom post tags :

    I did not find any built-in functionality to add custom mail tags, but I think there are several possible solutions here:

    1. Enable custom shortcodes (don't forget to create a shortcode handler in advance):

      • For mail components, according to DACrosby's answer.
      • For all mail (all components) using the 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 pre-populated data to the form, and then use it by mail: [my-custom-form-tag-with-some-prepopulated-data] ; the custom form tag must be registered with wpcf7_add_form_tag() as described above.
0
source

If someone is still looking for an answer in 2019, I would like to report that the Contact Form 7 WordPress plugin has performance issues and may reduce the performance of your websites. Often on our WordPress sites, elements are loaded that are not needed to load on certain pages or even everywhere. These resources (CSS and JavaScript files), as well as embedded code, increase the overall page size, so loading a page takes longer.

This can lead to a slow website that leads to page crashes, poor Google search rankings and sometimes conflicts with JavaScript errors when too many scripts load, and one of them (or more) has poorly written code that is not stand-alone and interacts poorly with other code.

The problem is that this plugin downloads 2 files (stylesheets and JavaScript) everywhere on your site, when most WordPress websites use them only on the contact page. These files:

/wp-content/plugins/contact-form-7/includes/css/styles.css?ver=5.1.4 (stylesheet file) / wp-content / plugins / contact-form-7 / includes / js / scripts. js? ver = 5.1.4 (JavaScript file) Why should I download these files when I'm on the home page of my site? Ideally, these files are needed only when I load my page that has a contact or my user form.

Here is a detailed article about the problem and how to fix it.

0
source

I haven’t done this before, but I think that shortcodes are managed by wordpress itself (even for plugins like CF7).

An example of creating a simple short code:

 //[foobar] function foobar_func( $atts ){ return "foo and bar"; } add_shortcode( 'foobar', 'foobar_func' ); 

Posted in functions.php.

For more information: http://codex.wordpress.org/Shortcode_API

Or you can use a plugin that does the job: http://wordpress.org/extend/plugins/shortbus/

-1
source

All Articles