Home » WordPress » Getting a value from Contact form 7 shortcode attribute

Getting a value from Contact form 7 shortcode attribute

To get the default value from shortcode attributes, add the default:shortcode_attr option to the form-tag:

Then, add an attribute with the same name as the field (“partner” in this case) into the shortcode for the contact form:

here I’m getting the Query String value and retrieves the partner field. you can place whatever value inside partner attribute

<?php  
$partnerIDString = $_SERVER['QUERY_STRING'];
$partnerName = get_the_title($partnerIDString);

echo apply_shortcodes( '[contact-form-7 id="1526" title="Package Inquiry Form" partner="'.$partnerName.'"]' ); 
?>

Now you need to register the attribute. Add the following code snippet to your functions.php file.

add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3 );

function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) {
    $my_attr = 'partner';
    if ( isset( $atts[$my_attr] ) ) {
        $out[$my_attr] = $atts[$my_attr];
    }
    return $out;
}

Reference

Duminda Wijerathna

An all round web designer building websites Over 18 years. Interested in achieving a suitable placement in the field of IT in a growth oriented organization which offers diverse job responsibilities in order to utilize and improve my skills, Knowledge and experience.

Leave a Comment

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