• Resolved ciprianolaru

    (@ciprianolaru)


    cf7 uses the following code to generate $unit_tag

    if ( in_the_loop() ) {
    			$unit_tag = sprintf( 'wpcf7-f%1$d-p%2$d-o%3$d',
    				absint( $id ), get_the_ID(), $global_count );
    		} else {
    			$unit_tag = sprintf( 'wpcf7-f%1$d-o%2$d',
    				absint( $id ), $global_count );
    		}

    conditional field for cf7 uses the following code to generate $unit tag
    $unit_tag = 'wpcf7-f'.$cf7form->id().'-p'.$post->ID.'-o'.$global_count;

    it works fine as long as the form is used in_the_loop().
    it fails to associate javascript to form when not used in_the_loop().

    you should update function wpcf7cf_enqueue_scripts($cf7form)
    with

    if ( in_the_loop() ) 
    {
    	$unit_tag = 'wpcf7-f'.$cf7form->id().'-p'.$post->ID.'-o'.$global_count;
    } 
    else 
    {
    	$unit_tag = 'wpcf7-f'.$cf7form->id().'-o'.$global_count;
    }

    basicaly, $unit_tag should not contain "-p" . $post->ID when used in a widget.

    • This topic was modified 8 years, 5 months ago by ciprianolaru. Reason: typo
Viewing 9 replies - 1 through 9 (of 9 total)
  • Thanks! This fixed my issue.

    Thread Starter ciprianolaru

    (@ciprianolaru)

    i’m glad it helped

    Hi,

    How do I update this function?

    Thread Starter ciprianolaru

    (@ciprianolaru)

    @phele, one fast way to fix it is to update the
    function wpcf7cf_enqueue_scripts($cf7form)
    (around line 180 in contact-form-7-conditional-fields.php)
    and to replace
    this
    $unit_tag = 'wpcf7-f'.$cf7form->id().'-p'.$post->ID.'-o'.$global_count;
    with this

    if ( in_the_loop() ) 
    	{
    		$unit_tag = 'wpcf7-f'.$cf7form->id().'-p'.$post->ID.'-o'.$global_count;
    	} 
    	else 
    	{
    		$unit_tag = 'wpcf7-f'.$cf7form->id().'-o'.$global_count;
    	}

    be aware!
    if you make changes that way (altering plugin files) they will be overwritten when / if updating this plugin (so you will need to reapply the fix if the maintainer doesn’t solve it by then).

    Thank you. I added the following code to the functions file and it seems to work.

    
    function wpcf7cf_enqueue_scripts_new($cf7form) {
    	if (in_the_loop()) {
    		$unit_tag = 'wpcf7-f'.$cf7form->id().'-p'.$post->ID.'-o'.$global_count;
    	} 
    	else {
    		$unit_tag = 'wpcf7-f'.$cf7form->id().'-o'.$global_count;
    	}
    }
    add_filter('wpcf7cf_enqueue_scripts', 'wpcf7cf_enqueue_scripts_new');
    

    Hi,
    Will the plugin’s author add this correction in the next version?
    Thank’s

    Thread Starter ciprianolaru

    (@ciprianolaru)

    hello @bvsmith,

    not sure about author’s intentions because i’m only a regular user of this plugin.
    hopefully he will include this fix in the next version (that’s why i reported it here).

    Thank’s for your response @ciprianolaru.
    If I find enough time, i’ll fork the project and work a little bit on it.

    Plugin Author Jules Colle

    (@jules-colle)

    Thanks for the contribution @ciprianolaru. I will include your code in the next update of the plugin.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘problem with $unit_tag when not in the loop (form not used in post or page)’ is closed to new replies.