• Resolved helppawz1

    (@helppawz1)


    HI,

    I use the following code on my site, and it now comes up with an error when the form is submitted (selected value does not exist). I think it may be because the forminator_replace_form_data hook has been modified. Please could you let me know how to change the code so it continues to work.

    Here’s the code:

    'forminator_replace_form_data', 
    	function( $content, $data, $fields ) {
    		if ( ! empty( $content ) ) {
    			return $content;
    		}
    
    		$form_fields = Forminator_API::get_form_fields( $data['form_id'] );
    		$data_field = '';
    		foreach($data as $key => $value){
    	    	if ( strpos( $key, 'select' ) !== false ) {
    	    		$values = '';
    	    		$field_value = isset( $data[ $key ] ) ? $data[ $key ] : null;
    
    		    	if ( ! is_null( $field_value ) ) {
    		    		$fields_slugs  = wp_list_pluck( $form_fields, 'slug' );
    					$field_key     = array_search( $key, $fields_slugs, true );
    					$field_options = false !== $field_key && ! empty( $form_fields[ $field_key ]->raw['options'] )
    							? wp_list_pluck( $form_fields[ $field_key ]->options, 'label', 'value' )
    							: array();
    
    					if ( ! isset( $field_options[ $field_value ] ) && isset( $_POST[ $key ] ) ) {
    						return sanitize_text_field( $_POST[ $key ] );
    					}
    				}
    			}
    		}
    	    return $content;
    	},
    	10,
    	3
    );

    thank you

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @helppawz1

    I hope you’re well today!

    The hook has been slightly changed but it shouldn’t affect your code as what changed is the 3rd argument which you are actually not using in code at all. The other two (which you are using) – the $content and $data – are still there as they were.

    However, I’m not quite sure where this error happens for you. Do you actually see it on front-end when submitting the form or it’s in submission content in back-end? Or does it show in debug.log (if it’s enabled)?

    Could you provide a screenshot of that error and also export the form in question so we could check how its set up and test on our end?

    To share the form it’s best to put export file on Google Drive, Dropbox or similar and share direct link to it with us in response below.

    Best regards,
    Adam

    Thread Starter helppawz1

    (@helppawz1)

    It shows on the front end of the form and doesn’t allow it to submit.

    Here’s a link to the form export and a screenshot of the error: file://LAPTOP-LGAN80PN/Users/vicky/OneDrive/Shared%20Forminator

    There is also another piece of code involved for this field, which is as follows:

    add_filter(
    	'forminator_cform_render_fields',
    	function( $wrappers, $model_id ) {
    		
    		$form_id = 12214;
            
    		if ( $form_id === $model_id ) {
    
    		$select_fields_data = array(
    			'select-1' => 'puppy_searcher',
    		);
    
    		foreach ( $wrappers as $wrapper_key => $wrapper ) {
    			if ( ! isset( $wrapper[ 'fields' ] ) ) {
    				continue;
    			}
    
    			if ( 
    				isset( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) &&
    				! empty( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] )
    			) {
    				$posts = get_posts( array( 'post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] , 'author'=> get_current_user_id(),) );
    
    				if ( ! empty( $posts ) ) {
    					$new_options = array();
    
    					foreach( $posts as $post ) {
    						$new_options[] = array(
    							'label' => $post->post_title,
    							'value' => get_permalink($post->ID),
    							'limit' => '',
    							'key'   => forminator_unique_key(),
    						);
    					}
    
    					$wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options;
    				}
    			}
    		}
    		}
    		
    
    		return $wrappers;
    	},
    	10,
    	2
    );
    
    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @helppawz1

    It seems that by mistake you’ve shared a path to the file on your local drive/computer.

    This isn’t something I could access ??

    Please grab an actual share link from OneDrive (publicly sharable link) and post in in response and we’ll continue assisting you.

    Best regards,
    Adam

    Thread Starter helppawz1

    (@helppawz1)

    Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @helppawz1,

    I imported the form to a test website but couldn’t replicate the issue.

    It looks like the code you initially shared is not copied entirely. Can you please share the complete code so we can take a closer look at this issue?

    Kind Regards,
    Nebu John

    Thread Starter helppawz1

    (@helppawz1)

    Hi,

    I have re-installed version 1.15.14 and the code works, so it is something to do with any updates after that. Here is the full code:

    add_filter(
    	'forminator_cform_render_fields',
    	function( $wrappers, $model_id ) {
    		
    		$form_id = 12214;
            
    		if ( $form_id === $model_id ) {
    
    		$select_fields_data = array(
    			'select-1' => 'puppy_searcher',
    		);
    
    		foreach ( $wrappers as $wrapper_key => $wrapper ) {
    			if ( ! isset( $wrapper[ 'fields' ] ) ) {
    				continue;
    			}
    
    			if ( 
    				isset( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) &&
    				! empty( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] )
    			) {
    				$posts = get_posts( array( 'post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] , 'author'=> get_current_user_id(),) );
    
    				if ( ! empty( $posts ) ) {
    					$new_options = array();
    
    					foreach( $posts as $post ) {
    						$new_options[] = array(
    							'label' => $post->post_title,
    							'value' => get_permalink($post->ID),
    							'limit' => '',
    							'key'   => forminator_unique_key(),
    						);
    					}
    
    					$wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options;
    				}
    			}
    		}
    		}
    		
    
    		return $wrappers;
    	},
    	10,
    	2
    );
    
    add_filter(
    	'forminator_replace_form_data', 
    	function( $content, $data, $fields ) {
    		if ( ! empty( $content ) ) {
    			return $content;
    		}
    
    		$form_fields = Forminator_API::get_form_fields( $data['form_id'] );
    		$data_field = '';
    		foreach($data as $key => $value){
    	    	if ( strpos( $key, 'select' ) !== false ) {
    	    		$values = '';
    	    		$field_value = isset( $data[ $key ] ) ? $data[ $key ] : null;
    
    		    	if ( ! is_null( $field_value ) ) {
    		    		$fields_slugs  = wp_list_pluck( $form_fields, 'slug' );
    					$field_key     = array_search( $key, $fields_slugs, true );
    					$field_options = false !== $field_key && ! empty( $form_fields[ $field_key ]->raw['options'] )
    							? wp_list_pluck( $form_fields[ $field_key ]->options, 'label', 'value' )
    							: array();
    
    					if ( ! isset( $field_options[ $field_value ] ) && isset( $_POST[ $key ] ) ) {
    						return sanitize_text_field( $_POST[ $key ] );
    					}
    				}
    			}
    		}
    	    return $content;
    	},
    	10,
    	3
    );

    There is a select field in the form which displays a list of cpt’s that have been created by the logged in user, for them to select and submit in the form.

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @helppawz1,

    Thanks for sharing the snippet. The code seems fine when checked on initial check.

    Regarding this line:

    			'select-1' => 'puppy_searcher',
    

    You are trying to populate the field with a Custom Post Type? Could you please confirm the slug “puppy_searcher” is correct? Does it work if you temporary change that to:
    'select-1' => 'post',

    Could you please share your form export so that we could have a better idea regarding the issue noticed?

    Please check the following doc on how to export a form:
    https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export

    You can share the export file via Google Drive, Dropbox or any cloud services in the next reply.

    Looking forward to your response.

    Kind Regards,
    Nithin

    Thread Starter helppawz1

    (@helppawz1)

    Hi Nithin,

    Yes, I am trying to populate the field with a custom post type. It still doesn’t work if I change it to ‘post’ instead.

    I have already exported the form – the link is above, but here it is again:

    https://1drv.ms/u/s!AkR-r0FkfxDnkKcMYe5CbZpipycqtw?e=cV1xDM

    The code does work on version 1.16.2 but not 1.17, so it must be something that has changed between the 2 versions.

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @helppawz1

    I hope you are doing well.

    Can you please try the updated version from this link?

    https://gist.github.com/patrickfreitasdev/d7ad926561db4b598baeaf66478d790b#file-custom-select-options-php

    I tested on Forminator 1.17.X and it is working well.

    Best Regards
    Patrick Freitas

    Thread Starter helppawz1

    (@helppawz1)

    That works, thank you so much!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Modified Hooks issue’ is closed to new replies.