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

    (@wpmudev-support8)

    Hi @tennyy

    I hope you’re well today!

    It’s pretty old code so it may require some update but let me just confirm first what exactly do you want to achieve.

    The code that you linked to is meant to work this way (it may be described slightly incorrectly):

    – it would compare current post ID (so page on which the form is located) with the array defined in the code
    – and if that post is matching one of the array elements, then the select option (already existing) defined in that array would be chosen.

    Is this what you are looking for – to have a select list with a set of options and just automatically select option based on what is the post ID of the current post/page with the form?

    Or are you actually looking to automatically generate the list of “select” field options based on the post list (e.g. fill select list with post titles and IDs of specific post type or category)?

    Please advise!

    Best regards,
    Adam

    Thread Starter tennyy

    (@tennyy)

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @tennyy,

    It appears the code seems to be outdated, so it won’t work as expected.

    As mentioned in the previous responses could you please confirm the exact workflow you are looking to achieve, so that we can check further based on that?

    Is this what you are looking for – to have a select list with a set of options and just automatically select option based on what is the post ID of the current post/page with the form?

    Or are you actually looking to automatically generate the list of “select” field options based on the post list (e.g. fill select list with post titles and IDs of specific post type or category)?

    Looking forward to your response.

    Kind Regads,

    Nithin

    Thread Starter tennyy

    (@tennyy)

    Hi,

    I have a order form on 5 pages with different products, but I want the form default selected the option(Product) when landing to the page.

    Eg. Page A – Television , Form option default selected “Television” instead of “Please select”.
    Page B – Toys , Form option default selected “Toys” instead of “Please select”.

    Thanks!

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @tennyy,

    So these are WooCommere products pages? ie Products > All Products where you are adding the form inside posts? or you referring to default Pages ie under Pages > All pages

    I’m afraid, there isn’t any out of the box setting for such a setup, however, you could check and see whether the following helps or not:

    <?php
    
    add_filter(
    	'forminator_cform_render_fields',
    	function( $wrappers, $model_id ) {
    		global $post;
    
    		if( $model_id != 361 ){
    			return $wrappers;
    		}
    
    		$select_fields_data = array(
    			'select-1' => 'product',
    		);
    
    		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' ] ] )
    			) {
    				$new_options = array();
    				if( 'product' == get_post_type( $post->ID ) ) {
    					$new_options[] = array(
    						'label' => $post->post_title,
    						'value' => $post->post_title,
    						'limit' => '',
    						'key'   => forminator_unique_key(),
    					);
    					$opt_data['options'] = $new_options;
    					$select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][0]['element_id'], true );
    					if( $select_field ){
    						if( $select_field['options'][0]['label'] != $opt_data['options'][0]['label'] ){
    							Forminator_API::update_form_field( $model_id, $wrapper['fields'][0]['element_id'], $opt_data );
    							$wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options;
    						}
    					}
    				}
    			}
    		}
    
    		return $wrappers;
    	},
    	10,
    	2
    );
    

    You’ll need to update the following line with the form ID:
    `

        if( $model_id != 21 ){

    Suppose the form ID is 123 then the above line will change as:

        if( $model_id != 123 ){

    This is the line which assigns the product to Select field:

    'select-1' => 'product',

    You’ll also have to make sure the “Select” field is empty ie remove any existing options before you apply the above code.

    Would highly recommend testing the code in a staging site.

    You can implement the above code via mu-plugins. Please check this link on how to implement the above code as a mu-plugins:
    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Best Regards,

    Nithin

    Thread Starter tennyy

    (@tennyy)

    Hi,

    I’m currently using a regular WordPress page to create a form. However, the updated code doesn’t provide a way to include the post ID, which means I’m unable to assign a default value to each post. Since the form is shared among multiple posts, this creates a limitation.

    Could you please assist me in finding a solution for this issue?

    Thank you!

    Thread Starter tennyy

    (@tennyy)

    Let me explain on another scenario to you. I have five different gym plans, each assigned to a separate page. However, all of these plans utilize the same form created by Forminator. My goal is to have the plan name automatically selected when a user visits the respective page. I attempted to implement a solution using the code provided in this GitHub Gist (https://gist.github.com/wpmudev-sls/adb493e7154f135fdf04db883e18927a), but unfortunately, it did not work as expected.

    {select-1} – Gym A / Gym B / Gym C / Gym D / Gym E

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @tennyy

    It seems then this code should cover this request, but as mentioned it is outdated and does not work anymore. I pinged our SLS Team to review this and update the code. We will post an update here as soon as more information is available.

    Kind Regards,
    Kris

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi again @tennyy

    We took a closer look on this code once more with our SLS Team https://gist.github.com/wpmudev-sls/adb493e7154f135fdf04db883e18927a and it seems to work as it should.

    Please let us know did you modifed this part of code

    // post_id => options value
    			15 => 'one',
    			27 => 'two',
    			63 => 'Option-3',

    Where 15, 27, and 63, should IDs of your pages and respective option values (not labels) should be automatically selected on post/page load.

    Kind Regards,
    Kris

    Thread Starter tennyy

    (@tennyy)

    Hi,

    I have identified three parts that need to be configured: field_idpost_id, and value.

    Currently, the compromise approach involves selecting the value through a direct link, such as?https://www.abc.com/test?select-1=gym-a.

    Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hello @tennyy

    Hope you’re doing well.

    Indeed, you will need to update the field_id rather the “select_field_id” in case there are multiple select fields added to your form.

    Further, for the select option to populate the correct value inside the select field based on the page where the form is placed, you will need to update the part of the code

    // post_id => options value
    			15 => 'one',
    			27 => 'two',
    			63 => 'Option-3',

    Using this approach it should work without the query string and the form should populate the correct value in the select field as per the page.

    Kind Regards,
    Saurabh

    Thread Starter tennyy

    (@tennyy)

    Hi,

    I have been making efforts to install a new WordPress instance on my cloud server in order to try a new fresh. However, I regret to inform you that I am facing the same issue as same as before. Despite my confidence in entering the correct data into the script, I have not achieved the desired outcome.

    I have been attempting to add a console log to the script, but it appears that the script is not running on the page, despite providing the correct post ID.

    Thanks!

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @tennyy,

    If these part is updated correctly in the Snippet, then it should have worked fine:

    		$select_field_id  = 'select-1';
    		$selected_options = array(
    			// post_id => options value
    			15 => 'one',
    			27 => 'two',
    			63 => 'Option-3',
    		);

    Possible to share the page URL where you have the form added and also the full code that you have applied on your website so that we could have a better idea?

    If you could also share the form export it would also be helpful.

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

    If you are concerned about any sensitive information in the form, then you can duplicate your form, remove any sensitive information, and then export it.

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

    Looking forward to your response.

    Best Regards,

    Nithin

    Thread Starter tennyy

    (@tennyy)

    HI,

    Sorry for late reply.

    I am pleased to inform you that I have finally identified the issue and found a solution.

    It turns out that the problem was related to the “Load form using AJAX” feature during rendering. Once I disabled this feature, I was able to observe the selected value being displayed automatically as intended.

    Unfortunately, below code will not run when I disable “Load form using AJAX”.

    <script>
    			 (function ($) {
    					 $(document).on("after.load.forminator", function (e, id) {
    			 //FWD Forms init
    			 
    							 $('.eng-name input').bind('focusout keyup', function () {
    									 var regx = /^[a-zA-Z'-'\s]+$/;
    									 var $parent = $(this).parent().parent();
    
    									 if (regx.test($(this).val()) || $(this).val() === '') {
    											 $parent.find('label.forminator-label--validation').remove();
    									 } else {
    											 if (!$parent.find('label.forminator-label--validation').length) {
    													 $parent.append('<label class="forminator-label--validation">Only English letters are allowed.</label>');
    											 }
    									 }
    							 });
    
    							 $('.phone-field input').bind('focusout keyup', function () {
    									 var regx = /^((2|4|5|6|9)\d{7})$|^((70|71|72|73|81|82|83)\d{6})$/;
    									 var $parent = $(this).parent().parent();
    
    									 if (regx.test($(this).val()) || $(this).val() === '') {
    											 $parent.find('label.forminator-label--validation').remove();
    									 } else {
    											 if (!$parent.find('label.forminator-label--validation').length) {
    													 $parent.append('<label class="forminator-label--validation">Please enter a valid phone number.</label>');
    											 }
    									 }
    							 });
    					 });
    			 })(window.jQuery);
    	 </script>
    Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @tennyy,

    Glad to hear you’ve identified the issue.

    We’re checking with the SLS techs, whether it would be possible to make the snippet work via Ajax.

    Unfortunately, below code will not run when I disable “Load form using AJAX”.

    – could you please test it after replacing this part:

    $(document).on("after.load.forminator", function (e, id) {

    with:

    $(document).bind('after.load.forminator ajaxComplete', function (e, id) {

    Best Regards,
    Dmytro

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘Auto populate fields with post id’ is closed to new replies.