How to add custom shortcodes in Mail body using contact form 7

Today we will discuss how we can add other plugin shortcodes in contact form 7 mail body. Currently the plugin only supports shortcodes inside the form itself, not inside mails. There may be other options available to do this but the "wpcf7_special_mail_tags" hook worked for us so we will go through with this hook.

wpcf7_special_mail_tags is the Contact Form 7 special mail tags hook.


Description

apply_filters( 'wpcf7_special_mail_tags',  $var,  $smt,  $false );


Parameters (3)

0. $var (string) => ''

The var.

1. $smt (string) => '_' . $smt

The smt.

2. $false (bool) => false

The false.


Usage:

To run the hook, copy the example below.

$var = apply_filters( 'wpcf7_special_mail_tags', $var, $smt, $false );             

if ( !empty( $var ) ) {             

   // everything has led up to this point...       

}


The following example is for adding a hook callback.

// define the wpcf7_special_mail_tags callback

function filter_wpcf7_special_mail_tags( $var, $smt, $false ) {

    // make filter magic happen here...

    return $var;

};

// add the filter

add_filter( 'wpcf7_special_mail_tags', 'filter_wpcf7_special_mail_tags', 10, 3 );


To remove a hook callback, use the example below.

// remove the filter

remove_filter( 'wpcf7_special_mail_tags', 'filter_wpcf7_special_mail_tags', 10, 3 );

 

Custom Shortcode



Here is the Working Example

I recently worked in one of my client websites where he wants to add the PDF Files Link in the mail body. So the task is Admin Upload PDF files from backend and when visitors submit the form from frontend then he/she will receive an email with PDF files Link.

Client wants all this process to be dynamic so that in future he can easily edit, add or remove the PDF's from the admin panel without modify the form code.

After lots of R&D we found a working solution that we will share with you so that it may be help you. We divided this in 2 parts.


First Part:

So firstly we need to find out the PDF Manager plugin that provides us features according to our requirements. I used the  BSK PDF Manager plugin for this because it will provide us many options that we need.


- Install and Activate the BSK PDF Manager plugin.

- After that create new categories according to your requirements.

- After that Add PDF / Documents inside that category.


First part is done.


Second Part:

In the second part we need to add the BSK PDF Manager shortcode in Contact Form 7 mail body. For this we will use the "wpcf7_special_mail_tags" hook. Here is the code:


function my_special_mail_tag( $output, $name, $html ) {

if ( 'pdfattach' == $name )

$output = do_shortcode( '[bsk-pdfm-category-ul id="1" order_by="date" order="DESC"]' );

 

return $output;

}

add_filter( 'wpcf7_special_mail_tags', 'my_special_mail_tag', 10, 3 );


Add that code in your theme functions.php file. This code will return all PDF files linked that exist under category Id 1.

After that add this shortcode [pdfattach] in your message body like this following screenshot.


Message-Body


Output in Email:


output In Email

That's it, just like above example you can add any plugin shortcode in contact form 7 mail body. We hope it will help you. If you have any issue or query please let us know through below comment box. Thanks!

Post a Comment

1 Comments

  1. Your Blogs is amazing.If you want to know that Contact Form 7 Not Sending Emails then click for more information:
    CONTACT FORM 7 NOT SENDING EMAILS

    ReplyDelete