How to get default values from Shortcode attributes CF7 WordPress

Have you ever wanted to use default values in Contact Form 7. That is not a really hard but it is something.

I am setting up default values for Name field and Email field. First you need Create the Form. Once you’re done with form lets do the trick.

This is the form I have created. You can see in the very beginning I have define the default attributes for Name field and the Email field. It is the way defining the default value. Please keep in mind this is different from the Placeholder value. Here what we are going to do is we are going to use this default value.

[text* your–name default:shortcode_attr]
[email* your–email default:shortcode_attr]

default:shortcode_attr is a compulsory option to the form tag, to get the default value. Once the attribute definition is done next you need to add those to the Contact Form short code. Field name should be same. Here is the way to call Short Code with the default values.

[contact–form–7 id="9" title="Contact Form" your–name="nerodev" your–email="[email protected]"]

Use your values instead of my values. But name field values should be the same. Now you’re done 60% of your work. You just need to add a filter to use the defined values. Here we go.

add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3 );
function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) {
    $your_name = 'your-name';
    $your_email = 'your-email';
    if ( isset( $atts[$your_name] ) ) {
        $out[$your_name] = $atts[$your_name];
    if ( isset( $atts[$your_email] ) ) {
        $out[$your_email] = $atts[$your_email];
    return $out;

In the function I have assigned name field and email field into the variables. So, we can continue with those variables. What you have to do is just check for the defined variables and return the value. You all can use this filter by changing the values and the variables where you want to change. That is all. You can just call the Form Shortcode and see the default values in the fields. So, here we get our final output as below.

You may also like...

2 Responses

  1. Khiang Seow says:

    Hmm. I followed the code here and my defaults aren’t filling the fields. What could be interfering with the function call? My theme is the Thrive Theme Builder .

Leave a Reply

Your email address will not be published. Required fields are marked *