Contact Form 7 – Sorin Coza http://localhost/sorincoza.com/blog-wp Just another web blog. Tue, 30 Aug 2016 10:46:43 +0000 en-US hourly 1 https://wordpress.org/?v=4.4.4 How to add custom shortcodes in CF7 http://localhost/sorincoza.com/blog-wp/how-to-add-custom-shortcodes-in-cf7/ Wed, 16 Dec 2015 17:59:22 +0000 http://sorincoza.com/blog/?p=57 Sometimes, the types of fields that are available by default in Contact Form 7 are just not enough. And sometimes, you want to just be able to use a shortcode when you define the form . For such situation, we can use the wpcf7_add_shortcode() function, combined with the wpcf7_init hook.

Documentation

The wpcf7_add_shortcode() function takes 3 arguments:

  • $shortcode, which is our shortcode name, exactly as in WordPress;
  • $callback, which is a function that will be called when the shortcode is encountered, exactly as in WordPress;
  • $has_name_part, which is boolean and false by default, but if it is set to true, then the shortcode can be used with a name, for example: '[my_shortcode field-name]', just how you would use a usual CF7 field: '[text* your-name]'

Find all details on the Contact Form 7 site’s documentation page: Adding a custom form-tag.

Example

add_action( 'wpcf7_init', 'create_my_custom_cf7_shortcodes' );

function create_my_custom_cf7_shortcodes(){
    wpcf7_add_shortcode( 'lots_of_checkboxes', 'render_checkboxes' );
}

function render_checkboxes(){
    // Construct checkboxes HTML here...
    return $html;
}

Now we can use this when we are defining the form fields: [lots_of_checkboxes], which will output whatever the render_checkboxes() function decides.

]]>
Useful hooks in CF7 http://localhost/sorincoza.com/blog-wp/useful-hooks-in-cf7/ Wed, 16 Dec 2015 17:25:27 +0000 http://sorincoza.com/blog/?p=50 Contact form 7 has a few hooks that you can use to customize your forms. Here is a list of those hooks that I think are the most helpful.

wpcf7_init

This is great if you want to add custom shortcodes to be handled by Contact Form 7. The shortcodes can be used in your Dashboard when you define the form fields. More details here: How to add custom shortcodes in CF7.

wpcf7_before_send_mail

This is fired just before the mail is sent. By this time, all validations have passed.

wpcf7_mail_sent

This is fired after the mail was sent. By this time, the $_POST is empty. Check this post for details on how to manipulate posted data in this hook: How to correctly get posted data in CF7.

wpcf7_validate_{$field_type}

This is a filter that helps us to validate fields using our own custom rules. Read this post for more information on how to do that: How to validate a field in CF7.

]]>
How to correctly get posted data in CF7 http://localhost/sorincoza.com/blog-wp/how-to-correctly-get-posted-data-in-cf7/ Wed, 16 Dec 2015 17:08:44 +0000 http://sorincoza.com/blog/?p=46 In Contact Form 7, if you hook into wpcf7_mail_sent, then  the $_POST variable is empty, so you cannot use it to do stuff with it. You are too late. However, there is a way to get around this: you have to use Contact Form 7 built-in functions to get the posted data.

$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
    $form_data = $submission->get_posted_data();

    // now $form_data contains the form values,
    // so you can do, for example $form_data['email'], instead of $_POST['email']

}else{
    // we got nothing; something went wrong
}

That’s it.

]]>
How to validate a field in CF7 http://localhost/sorincoza.com/blog-wp/how-to-validate-a-field-in-cf7/ Wed, 16 Dec 2015 16:46:56 +0000 http://sorincoza.com/blog/?p=36 What we need:

  • a filter 'wpcf7_validate_{$field_type}', which will filter the fields by type;
  • a function, which takes 2 arguments from Contact Form 7:
    • $result, which can be validated or invalidated before it is returned;
    • $tag, which helps us to identify the field by its name.

The {$field_type} is the exact field type that you have defined in your form, for example: text, text*, textarea, textarea*, email, etc.... The asterisk is used to indicate a required field, and yes, it has to be specified in the hook ( text is different from text* ).

Example

// hook our function:
add_filter( 'wpcf7_validate_text*', 'validate_phone', 20, 2 );

// define the function:
function validate_phone( $result, $tag ){

    // check if this is our phone field, and return the $result unchanged if it isn't:
    $tag = new WPCF7_Shortcode( $tag );
    if ( $tag->name !== 'phone' ){ return $result; }

    // if we got this far, then this is our phone input - we check to see if it is valid
    // assuming we have a function somewhere named "is_valid_phone"
    if( ! is_valid_phone('phone') ){
        $result->invalidate( $tag, 'This phone number is not valid! Please try again.' );
    }

    // finally, return the result:
    return $result;
}
]]>