Below, you’ll find a detailed guide on how to add the Smart Grid-Layout Design for Contact Form 7 Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Smart Grid-Layout Design for Contact Form 7 Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Smart Grid-Layout Design for Contact Form 7 Plugin and the shortcodes it provides:
"Smart Grid-Layout Design for Contact Form 7 is an innovative plugin that enhances your website's contact forms. It allows you to design form layouts in a grid style, making your forms more organized and user-friendly."
- [cf7-form]
Smart Grid-Layout Design for Contact Form 7 [cf7-form] Shortcode
The CF7-Grid-Layout plugin shortcode ‘cf7-form’ is used to display a specific Contact Form 7 form on your WordPress site. The shortcode function merges default attributes with user-defined ones. It checks if the ‘cf7key’ attribute is empty, returning an error message if it’s missing. It then applies filters to include hidden form fields, separates attributes, and fetches the form post with the given ‘cf7key’. If the form exists, it appends attributes to the form shortcode and returns it for rendering. If the form doesn’t exist, it returns an error message. This shortcode also supports form field pre-filling and language specification.
Shortcode: [cf7-form]
Parameters
Here is a list of all possible cf7-form shortcode parameters and attributes:
cf7key
– key attribute of the cf7 form to displaylang
– language attribute of the form, if different versions existcf7sg
– prefix for custom attributes, used for hidden fields and other parameterspost_type
– identifies the type of content, which should be a cf7 formname
– unique name of the form in the posts table
Examples and Usage
Basic example – The following shortcode displays a CF7 form with the key attribute set to ‘contact-us’. This will load the form with the key ‘contact-us’.
[cf7-form cf7key="contact-us" /]
Advanced examples
Display a CF7 form with the key attribute set to ‘contact-us’ and language set to ‘fr’. This will load the form with the key ‘contact-us’ in French.
[cf7-form cf7key="contact-us" lang="fr" /]
Display a CF7 form with the key attribute set to ‘contact-us’, language set to ‘fr’, and a hidden field ‘user_id’ with value ‘123’. This will load the form with the key ‘contact-us’ in French and include a hidden field ‘user_id’ with value ‘123’.
[cf7-form cf7key="contact-us" lang="fr" cf7sg/hidden/user_id="123" /]
Finally, this shortcode displays a CF7 form with the key attribute set to ‘contact-us’, a hidden field ‘user_id’ with value ‘123’, and a custom field ‘name’ with value ‘John Doe’. This will load the form with the key ‘contact-us’, include a hidden field ‘user_id’ with value ‘123’, and prefill the ‘name’ field with ‘John Doe’.
[cf7-form cf7key="contact-us" cf7sg/hidden/user_id="123" cf7sg/name="John Doe" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [cf7-form]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'cf7-form', array( $cf7_admin, 'shortcode') );
Shortcode PHP function:
function shortcode( $atts ) {
$a = array_merge( array(
'cf7key' => '',
), $atts );
if(empty($a['cf7key'])){
return '<em>' . __('cf7-form shortcode missing key attribute','cf7-grid-layout') . '</em>';
}
/** @since 4.4.0 enable field values */
$hidden = apply_filters('cf7sg_include_hidden_form_fields', array(),$a['cf7key']);
$fields = array();
foreach($atts as $key=>$atts_val){
$field = explode('/',$atts_val);
if(is_array($field) && 'cf7sg'==$field[0]){
switch(count($field)){
case 2:
$field = explode('=',$field[1]);
$fields[$field[0]] = isset($field[1]) ? trim($field[1],'"'):'';
break;
case 3:
if('hidden'==$field[1]){
$field = explode('=',$field[2]);
$hidden[$field[0]] = isset($field[1]) ? trim($field[1],'"'):'';
}
break;
}
unset($a[$key]);
}
}
//else get the post ID
$args = array(
'post_type' => self::cf7_post_type(),
'name' => $a['cf7key'],
);
if(isset($a['lang'])) $args['lang'] = $a['lang'];
$form = get_posts($args);
if(!empty($form)){
$id = $form[0]->ID;
if( !isset($a['lang']) ){ /** @since 4.4 allow different lang */
$id = apply_filters('cf7_form_shortcode_form_id',$id, $atts);
}
wp_reset_postdata();
$attributes ='';
foreach($a as $key=>$value){
$attributes .= ' '.$key.'="'.$value.'"';
}
/** @since 4.4.0 diffrentiate preview forms */
if( isset($_GET['post_type']) && 'cf7sg_page'==$_GET['post_type'] && isset($_GET['preview']) ){
$hidden['_cf7sg_preview']=true;
if(isset($_COOKIE['_cf7sg_'.$a['cf7key']]) and apply_filters('cf7sg_preview_prefill', true, $a['cf7key'])){ /** @since 4.15.0 */
$fields = array_merge( json_decode( stripslashes($_COOKIE['_cf7sg_'.$a['cf7key']]), true),$fields);
}
}
if(!empty($hidden)){
add_filter('wpcf7_form_hidden_fields', function($hide) use ($hidden, $id) {
$form = wpcf7_get_current_contact_form();
// debug_msg($hidden, "$form->id() add hidden " );
if(empty($form)) return $hide;
if($form->id()!=$id) return $hide;
return array_merge($hide, $hidden);
},PHP_INT_MAX,1);
}
if(!empty($fields)){ /** @since 4.4.0 */
add_filter('cf7sg_preview_form_fields', function($pairs, $key) use ($fields, $a){
if($a['cf7key']==$key) return array_merge($fields, $pairs);
},PHP_INT_MAX,2);
}
return do_shortcode('[contact-form-7 id="'.$id.'"'.$attributes.']');
}else{
return '<em>' . __('cf7form shortcode key error, unable to find form, did you update your form key?','cf7-grid-layout') . '</em>';
}
}
Code file location:
cf7-grid-layout/cf7-grid-layout/assets/cf7-admin-table/cf7-admin-table-loader.php
Conclusion
Now that you’ve learned how to embed the Smart Grid-Layout Design for Contact Form 7 Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply