WordPress.org

Ready to get started?Download WordPress

Forums

Contact Form 7
Custom shortcodes in forms to dynamically generate CF7 fields (2 posts)

  1. Webartisan
    Member
    Posted 1 year ago #

    Hi,
    I've build a custom schortcode (i.e. [create_restricted_fields]) that I can use in the creation of a C7 form and that gets rendered on the page as some text fields only if certain conditions are met (if not the text fields are just not generated).

    The shortcode works thanks to this small function that was added in functions.php

    add_filter( 'wpcf7_form_elements', 'shortcodes_in_cf7' );
    
      // This is required to execute shortcodes in CF7 FORMS
    	function shortcodes_in_cf7( $form ) {
    		$form = do_shortcode( $form );
    		return $form;
    	}

    The actual shortcode is located in my plugin and it's something like this:

    add_shortcode('create_restricted_fields', 'myplugin_form_elements_address');
    
    function myplugin_form_elements_address() {
     //... sql checks $test
    
     $result = ($test==true) ? wpcf7_do_shortcode('[text* your-address 20/30 id:your-address placeholder "Address*"]') : '';
    
    //... other fields with the same logic
    
    return $result
    }

    The fields get rendered as desidered like the normal fields added in CF7 form creation, with all the expected wpcf7's span and classes :

    <span class="wpcf7-form-control-wrap your-address"><input type="text" name="your-address" value="" size="20" maxlength="30" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" id="your-address" aria-required="true" placeholder="Address*" /></span>

    Now the problem is that although it has all the attributes and classes related to validation it is ignored by the validation on submit and gets validated even if left empty, so the form is submitted without an important (required) data.

    I suspect the problem is WHEN the custom shortcode is processed, that maybe happen after some other fields initialisation is done (just a hypothesis) or perhaps the wpcf7_do_shortcode() function used in my shortcode is not the correct one.

    Thanks to anyone who can help with this issue. If it's something already discussed please tell me because it's hard searching for it without a specific search engine in the wp.org forums.

    http://wordpress.org/extend/plugins/contact-form-7/

  2. Webartisan
    Member
    Posted 1 year ago #

    Hello,
    I'm still struggling with this. I understand that maybe I should add my fields to the global $wpcf7_contact_form in my (shortcode)function but I can't figure out how. Perhaps plugin's author could give a hint (if interested). Thank you.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic