Support » Plugin: Contact Form 7 » Getting Default Values from Shortcode Attributes

  • I want to include the “hotelmotel” attribute in the shortcode and for it to display on the mailed form, but I’ve got something wrong. Can someone help please.

    There’s a great explanation how to do it here:
    https://contactform7.com/getting-default-values-from-shortcode-attributes/

    Added this to the bottom of functions.php:

    /* Add hotelmotel shortcode attribute */
    add_filter( ‘shortcode_atts_wpcf7’, ‘custom_shortcode_atts_wpcf7_filter’, 10, 3 );

    function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) {
    $my_attr = ‘hotelmotel’;

    if ( isset( $atts[$my_attr] ) ) {
    $out[$my_attr] = $atts[$my_attr];
    }

    return $out;
    }

    Added this into the Mail message body:

    [hotelmotel]

    Added this to the form:

    [hidden text hotelmotel default:shortcode_attr]

    Added this on the page where we want the form to appear:

    [contact-form-7 id=”38″ title=”Contact form – Andy” hotelmotel=”Empire Hotel Gympie”]

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Added this to the form:

    [hidden text hotelmotel default:shortcode_attr]

    The name of a form-tag is the second word in the tag. In the case of the tag above, “text” is its name.

    Thanks. But it’s still not working sorry. Help please!

    functions.php:

    /* Add hotelmotel shortcode attribute */
    add_filter( ‘shortcode_atts_wpcf7’, ‘custom_shortcode_atts_wpcf7_filter’, 10, 3 );

    function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) {
    $my_attr = ‘hotelmotel’;

    if ( isset( $atts[$my_attr] ) ) {
    $out[$my_attr] = $atts[$my_attr];
    }

    return $out;
    }

    Mail message body:
    [hotelmotel]

    Form:
    [hidden hotelmotel default:shortcode_attr]

    Page:
    [contact-form-7 id=”38″ title=”Contact form – Andy” hotelmotel=”Empire Hotel Gympie”]

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    What other plugins and theme do you use on the site?

    I’ve tried deactivating every plugin except Contact Form 7. But still no luck 🙁

    Thanks for your help!

    WP 4.9.10
    Theme: DiviVersion 3.12.2 By Elegant Themes

    Admin Post Navigation 2.1 By Scott Reilly
    BackupBuddy 6.1.0.2 By iThemes
    Contact Form 7 5.1.1 By Takayuki Miyoshi 🙂
    Divi Booster By 1.2.4 Dan Mossop
    Divi Children 2.0.7.1 By Luis Alejandre
    Duplicate Post 3.2.2 By Enrico Battocchi
    Elegant Themes Updater 1.1 By Elegant Themes
    Google Sitemap 3.1.7 by BestWebSoft By BestWebSoft
    Insert Pages 3.4.5 By Paul Ryan
    Intuitive Custom Post Order 3.1.2 By hijiri
    List category posts 0.79 By Fernando Briano
    Really Simple SSL 3.15 By Rogier Lankhorst
    Yoast Version 10.1.3

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    I don’t see any mistakes in the current configuration you’ve posted and have no idea why it isn’t working. Not sure, but my gut tells there is something wrong around the functions.php code.

    Would the child functions.php help?

    <?php
    /**
    * Functions – Child theme custom functions
    */

    /*****************************************************************************************************************
    Caution: do not remove this or you will lose all the customization capabilities created by Divi Children plugin */
    require_once(‘divi-children-engine/divi_children_engine.php’);
    /****************************************************************************************************************/

    /**
    * Patch to fix Divi issue: Duplicated Predefined Layouts.
    */
    remove_action( ‘admin_init’, ‘et_pb_update_predefined_layouts’ );
    function Divichild_pb_update_predefined_layouts() {
    if ( ‘on’ === get_theme_mod( ‘et_pb_predefined_layouts_updated_2_0’ ) ) {
    return;
    }
    if ( ! get_theme_mod( ‘et_pb_predefined_layouts_added’ ) OR ( ‘on’ === get_theme_mod( ‘et_pb_predefined_layouts_added’ ) )) {
    et_pb_delete_predefined_layouts();
    }
    et_pb_add_predefined_layouts();
    set_theme_mod( ‘et_pb_predefined_layouts_updated_2_0’, ‘on’ );
    }
    add_action( ‘admin_init’, ‘Divichild_pb_update_predefined_layouts’ );

    function mycustom_featured_width( ) { return 160; /* Custom featured post image width */ }
    add_filter( ‘et_pb_blog_image_width’, ‘mycustom_featured_width’);
    add_filter( ‘et_pb_index_blog_image_width’, ‘mycustom_featured_width’);

    function mycustom_featured_height( ) { return 130; /* Custom featured post image height */ }
    add_filter( ‘et_pb_blog_image_height’, ‘mycustom_featured_height’);
    add_filter( ‘et_pb_index_blog_image_height’, ‘mycustom_featured_height’);

    function mycustom_featured_size( $image_sizes ) {
    $custom_size = mycustom_featured_width() . ‘x’ . mycustom_featured_height();
    $image_sizes[$custom_size] = ‘et-pb-post-main-image-thumbnail’;
    return $image_sizes;
    }
    add_filter( ‘et_theme_image_sizes’, ‘mycustom_featured_size’ );

    /* Index & archive */
    function wpt_index_thumbnail_width( $width ) {
    if( !is_single() ) {
    return 150; //index thumbnail width in pixels
    } else {
    return $width;
    }
    }
    add_filter( ‘et_pb_index_blog_image_width’, ‘wpt_index_thumbnail_width’);
    function wpt_index_thumbnail_height( $height ) {
    if( !is_single() ) {
    return 212; //index thumbnail height in pixels
    } else {
    return $height;
    }
    }
    add_filter( ‘et_pb_index_blog_image_height’, ‘wpt_index_thumbnail_height’);

    /* Add hotelmotel shortcode attribute */
    add_filter( ‘shortcode_atts_wpcf7’, ‘custom_shortcode_atts_wpcf7_filter’, 10, 3 );

    function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) {
    $my_attr = ‘hotelmotel’;

    if ( isset( $atts[$my_attr] ) ) {
    $out[$my_attr] = $atts[$my_attr];
    }

    return $out;
    }

    ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Getting Default Values from Shortcode Attributes’ is closed to new replies.