• Resolved alainmelsens

    (@alainmelsens)


    Hello,
    I have found very interesting API PHP scripts examples how certain Forminator fields can be updated using API. But I am not finding anything about how select fields can be updated.
    I would like to be able to update the select options but it does not work.
    Thanks for providing an example or telling me where on the Internet I can find an example API script on how to update a select field including the select options. Or where can I find information to update all possible fields separately via API?
    Or does anyone have experience with this and can tell me how to do it?
    I am not a PHP programmer expert. But I do know how to use a WordPress MU plugin or I have some experience with the WordPress “Code Snippets” plugin to use separate PHP scripts.
    Thanks in advance.
    Kind Regards.

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

    (@wpmudev-support9)

    Hello @alainmelsens !

    I hope all is well.

    The main API Docs can be found here: https://wpmudev.com/docs/api-plugin-development/forminator-api-docs/

    But if you’d like us to explain how to do something, please share an example use case – what you’d like to achieve and we’ll ask our SLS team to provide some example snippets.

    I’ll also forward your request to the Documentation team so they can maybe add some examples to the docs I’ve shared.

    Kind regards,
    Pawel

    Thread Starter alainmelsens

    (@alainmelsens)

    Hello Pawel,
    Indeed, I have already been able to solve some things with the help of the API documentation.
    But not all fields have the same characteristics and therefore I have a hard time updating e.g. a select field via API.
    How are the select options entered into the appropriate array?
    I thought using PHP code to study the script found the following would work but I cannot.

    $form_id = 7;
    $field_id = 'select-1';
    
    $data = array(
    	'id' 		=> 'select-1',
    	'element_id'	=> 'select-1',
    	'form_id' 	=> 'wrapper-1311247712118-1294',
        	"type"        	=> "select",
            "cols"     	=> "12",
            "required"    	=> "true",
            "value_type"  	=> "single",
            "field_label" 	=> "Make a selection",
            "placeholder" 	=> "Select an item",
           	"options"     	=> $options,
    	);
                   $options[] = array(
                        'label' 	=> "Label 1",
                        'value' 	=> "First item",
                        'limit' 	=> '',
                        'key'   	=> forminator_unique_key(),
                        'label' 	=> "Label 2",
                        'value' 	=> "Second item",
                        'limit' 	=> '',
                        'key'   	=> forminator_unique_key(),
                        'label' 	=> "Label 3",
                        'value' 	=> "Third item",
                        'limit' 	=> '',
                        'key'   	=> forminator_unique_key(),
                    );
    	
    	$fields = Forminator_API::update_form_field( $form_id, $field_id, $data );
    

    Can someone set up this example script correctly so that the existing select-1 field is modified or updated with new labels and also with new values? And of course be saved in the current form afterwards as well.
    I don’t know if the key settings are needed and if this forminator_unique_key() function is needed?
    Thanks in advance.
    Kind Regards.

    Plugin Support Adam – WPMU DEV Support

    (@wpmudev-support8)

    Hi @alainmelsens

    Thanks for response!

    Take a look here, please:

    https://gist.github.com/wpmudev-sls/fd71b5528ed894ebd5b42f5afbcf61e7

    It’s a slightly different approach but it works out of the box and should give you some insights. It’s a complete script to “bulk update” select field but it’s relatively simple so, I believe, you should be able to “cut out” what you need from it.

    To test it on site simply add it as MU plugin and make sure to set correct form ID and field ID in these lines:

    `private $form_id = 26;
    private $field_id = ‘select-1’;`

    Then you can define your own custom options (in form of “value” => “label”) to be added here:

    private $options = array(
                'value-1'    => 'Label 1',
                'value-2'    => 'Label 2',
                'value-3'    => 'Label 3',
            );

    Best regards,
    Adam

    Thread Starter alainmelsens

    (@alainmelsens)

    Hi Adam,
    I have tried the folow script as MU plugin, but I receive a critical error and the website is not working.

    
    <?php
    
    $form_id = 1269;
    $field_id = 'select-1';
    
    private $options = array(
                'value-1'    => 'Label 1',
                'value-2'    => 'Label 2',
                'value-3'    => 'Label 3',
            );
    
    $data = array(
    	'id' 			=> 'select-1',
    	'element_id'	=> 'select-1',
    	'form_id' 		=> 'wrapper-1311247712118-1294',
        "type"        	=> "select",
        "cols"       	=> "12",
        required"    	=> "true",
        "value_type"  	=> "single",
        "field_label" 	=> "Make a selection",
        "placeholder" 	=> "Select an item",
        "options"     	=> $options,
    	);
    	
    	$fields = Forminator_API::update_form_field( $form_id, $field_id, $data );
    	
    

    What did I wrong? Any suggestions, please?
    Thanks in advance to help me with this PHP script.
    Best Regards.

    • This reply was modified 2 years, 6 months ago by alainmelsens.
    Thread Starter alainmelsens

    (@alainmelsens)

    Hi,
    I tested further and got no more critical error and almost succeeded in changing the select options, except that only part of the options labels are filled in. There is apparently only 1 character per option. And afterwards, nothing was saved with the form for this selection. See screencapture here.
    Now my script is as follow:

    
    
    $form_id = 1269;
    $field_id = 'select-1';
    
    $data = array(
    	'id' 			=> 'select-1',
    	'element_id'	=> 'select-1',
    	'form_id' 		=> 'wrapper-1311247712118-1294',
        "type"        	=> "select",
        "cols"       	=> "12",
        "required"    	=> "true",
        "value_type"  	=> "single",
        "field_label" 	=> "Make a selection",
        "placeholder" 	=> "Select an item",
        "options"     	=> array(
                'value-1'    => 'Label 1',
                'value-2'    => 'Label 2',
                'value-3'    => 'Label 3',
            )
    	);
    	
    	$fields = Forminator_API::update_form_field( $form_id, $field_id, $data );
    

    I think I have almost found the solution. I really hope that someone can help me with this. I can’t see what could be wrong myself. Perhaps it is a small detail that is important.
    Thanks in advance.
    Kind regards

    • This reply was modified 2 years, 6 months ago by alainmelsens.
    • This reply was modified 2 years, 6 months ago by alainmelsens.
    Plugin Support Pawel – WPMU DEV Support

    (@wpmudev-support9)

    Hello @alainmelsens !

    Our developers took a look at your current code and they suggest to make a change to the options element in the following way:

    'options'     => array(
        array(
    	'label' => 'Option 1',
    	'value' => 'one',
    	'limit' => '',
        ),
        array(
    	'label' => 'Option 2',
    	'value' => 'two',
    	'limit' => '',
        ),
    ),

    So it’s actually an array of arrays with some additional details in them.

    Kind regards,
    Pawel

    Thread Starter alainmelsens

    (@alainmelsens)

    Hi Pawel,
    OK!!! YES!!!
    It works perfectly now!
    Thank you very much for this fantastic and brilliant support!
    I am so happy that it works.
    Normally the Forminator forms are not really multilingual yet. But thanks to these genius API methods, I think I can make it multilingual now. It is mainly the layout but also such select options that need to be translated.
    Thanks again for this fantastic support! I really appreciate it. I would not have found this myself.
    This support ticket may now be closed.
    Kind regards,
    Alain

    Thread Starter alainmelsens

    (@alainmelsens)

    Hi Pawel,
    I know I closed this ticket, but I did have another question regarding such API PHP code scripts for e.g. the GDPR checkbox field.
    In this field there is an extra small text editor where it is possible to modify a standard line of text with the following text:
    “Yes, I agree with the privacy policy and terms and conditions”.
    How can this be updated using an API script?
    What are the Array elements in which variable data can be added?
    Is this the right syntax to edit it?

    
    $form_id = 1269;
    $field_id = 'gdprcheckbox-1';
    
    $data = array(
    	'id' 			=> 'gdprcheckbox-1',
    	'element_id'	=> 'gdprcheckbox-1',
    	'form_id' 		=> 'wrapper-1311247712118-1094',
        "type"        	=> "gdprcheckbox",
        "cols"       	=> "12",
        "required"    	=> "true",
        "options"     	=> "This is GDPR phrase."
    	);
    
    	$fields = Forminator_API::update_form_field( $form_id, $field_id, $data );
    
    

    This script that I have tried, is not working, but no error messages, either.
    So, maybe the Array element that I have tried as “options” is not the good one.
    Please, which is the array element I should use for this? Or maybe there are other elements also to add?
    Thanks in advance.
    Kind Regards.

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @alainmelsens

    Can you try this?

    https://gist.github.com/patrickfreitasdev/5c79d4c7b2f64dc3b1b1b3a6fdfc1f58

    There is an upcoming update to the GDPR, it is going to be replaced by the Consent field so I added an example to the new version too.

    But it is mainly:

    'consent_description' => 'Your description'

    For GDPR:

    'gdpr_description' => 'Your description'

    Best Regards
    Patrick Freitas

    Thread Starter alainmelsens

    (@alainmelsens)

    Hi Patrick,
    Yes, this is also working fine. Thank you very much.
    I also want to be able to update the following primary field types via API.
    This includes radio buttons and check buttons. Are the array elements the same as for a select field?
    I don’t have some examples of these fields yet but I will test this either.
    Anyway, thank you again for your fast and great support.
    I will return soon with my experiences for radio and check buttons, if it will work also or not?
    Best Regards,
    Alain

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @alainmelsens

    You are welcome.

    This includes radio buttons and check buttons. Are the array elements the same as for a select field?

    To make sure are you referring to GDPR field or the Radio and Checkboxes fields itself?

    The GDRP is a simple checkbox option where we can update the text and include URLs to some privacy pages.

    Best Regards
    Patrick Freitas

    Thread Starter alainmelsens

    (@alainmelsens)

    Hi Patrick,

    I also want to be able to update the following primary field types via API.
    This includes radio buttons and check buttons. Are the array elements the same as for a select field?

    No, I mean other primary form fields like the radio buttons and check buttons. This has nothing to do with my previous question for the GDPR field, as this has been resolved. For which I sincerely thank you again.

    I just wanted to ask if any other radio button field or check buttons field also has the same options for this the same API – Array elements in the PHP code?
    See example for radio buttons:

    
    $form_id = 1269;
    $field_id = 'radio-1';
    
    $data = array(
    	'id' 			=> 'radio-1',
    	'element_id'	=> 'radio-1',
    	'form_id' 		=> 'wrapper-1311247712118-1295',
        "type"        	=> "select",
        "cols"       	=> "12",
        "required"    	=> "true",
        "value_type"  	=> "single",
        "field_label" 	=> "Make a selection",
        "placeholder" 	=> "Select an item",
    	   'options'     => array(
    	    array(
    		'label' => 'Option 1',
    		'value' => 'one',
    		'limit' => '',
    	    ),
    	    array(
    		'label' => 'Option 2',
    		'value' => 'two',
    		'limit' => '',
    	    ),
    	),
    );
    	
    	$fields = Forminator_API::update_form_field( $form_id, $field_id, $data );
    

    And for check buttons, the follow example:

    
    $form_id = 1269;
    $field_id = 'check-1';
    
    $data = array(
    	'id' 			=> 'check-1',
    	'element_id'	=> 'check-1',
    	'form_id' 		=> 'wrapper-1311247712118-1296',
        "type"        	=> "select",
        "cols"       	=> "12",
        "required"    	=> "true",
        "value_type"  	=> "single",
        "field_label" 	=> "Make a selection",
        "placeholder" 	=> "Select an item",
    	   'options'     => array(
    	    array(
    		'label' => 'Option 1',
    		'value' => 'one',
    		'limit' => '',
    	    ),
    	    array(
    		'label' => 'Option 2',
    		'value' => 'two',
    		'limit' => '',
    	    ),
    	),
    );
    	
    	$fields = Forminator_API::update_form_field( $form_id, $field_id, $data );
    

    I didn’t have the time yet to check these examples but I guess is maybe that what I mean. Should this work for radio buttons and check buttons or is are there other options array elements that I need to wrote in the API code?
    Thanks in advance.
    Best Regards,
    Alain

    Thread Starter alainmelsens

    (@alainmelsens)

    Hi Patrick,
    I suspect indeed that I have learnt it well… πŸ˜‰
    I have tried it myself and it seems to work.
    See below for the examples that already work for me.

    For check-buttons:

    
    add_action('init',function(){
    
    if( !class_exists( 'Forminator' ) ){
    return false;
    }
    
    $form_id = 1269;
    
    $field_id = 'checkbox-1';
    
    $data = array(
    'id' 			          => 'checkbox-1',
    'element_id'	      => 'checkbox-1',
    'type'        	    => "checkbox",
    'cols'       	      => "12",
    "field_label" 	=> "Check your selection",
    	'options'     => array(
    		array(
    		'label' => 'Check 1',
    		'value' => 'one',
    		'limit' => '',
    		),
    		array(
    		'label' => 'Check 2',
    		'value' => 'two',
    		'limit' => '',
    		),
    	),	
    );
    
    $fields = Forminator_API::update_form_field( $form_id, $field_id, $data );
    
    }, 20 );
    
    

    And for radio-buttons:

    
    add_action('init',function(){
    
    if( !class_exists( 'Forminator' ) ){
    return false;
    }
    
    $form_id = 1269;
    
    $field_id = 'radio-1';
    
    $data = array(
    'id' 			          => 'radio-1',
    'element_id'	      => 'radio-1',
    'type'        	    => "radio",
    'cols'       	      => "12",
    "field_label" 	=> "Check your radio button",
    	'options'     => array(
    		array(
    		'label' => 'Radiobutton 1',
    		'value' => 'one',
    		'limit' => '',
    		),
    		array(
    		'label' => 'Radiobutton 2',
    		'value' => 'two',
    		'limit' => '',
    		),
    	),	
    );
    
    $fields = Forminator_API::update_form_field( $form_id, $field_id, $data );
    
    }, 20 );
    
    

    But please, if you see or know of anything that can make my code even better with some practical and very useful additional API array elements, you may always let me know.
    You’re the expert together with your colleagues.

    It is a pity that there is not yet a handy syllabus with a fully comprehensive overview of all Forminator form field types with their separate API array elements and their properties.
    Currently, there exist a simple online API documentation, but only with one small example of a single field type.
    Currently there are 25 standard field types such as:
    Name, Email, Phone, Address, Website, Input, Textarea, Number, Radio, Checkbox, Calculations, Select, Datepicker, Timepicker Fiel Upload, Post Data, reCaptche, HTML, Page Break, Hidden Filed, Section, GDPR Approval, Currency, Stripe and PayPal.
    Some are indeed similar and can therefore be related. But others are completely different, which makes it very difficult to work this out without additional API code examples or additional support from you.
    This was exactly the initial goal of my question.

    I suppose I have already obtained a lot of extra code that I can use now.
    If you can help me further with some extra info or extra API example code, I will certainly be grateful for that.
    Thank you again for the very useful support you have already given me.
    Best Regards,
    Alain

    Plugin Support Adam – WPMU DEV Support

    (@wpmudev-support8)

    Hi @alainmelsens

    Thanks for updating us and I’m sorry for long response time.

    I’m glad to know that you was able to test and confirm that it works.
    As for your suggestions on docs, we are constantly improving our docs in general so I’ll also pass your feedback to our docs team.

    Thank you!

    Best regards,
    Adam

    Thread Starter alainmelsens

    (@alainmelsens)

    Hello,
    Is it possible to tell me which is the array variable name when using API to set a selection option to default selected? And how to set it as selected?
    Now I already know the following variables:

    
    array(
    		'label' => 'radiobutton 2',
    		'value' => 'two',
    		'limit' => '',
    		),
    

    I tried adding : ‘selected’ => ‘1’, see below:

    
    array(
    		'label' => 'Radiobutton 2',
    		'value' => 'two',
    		'limit' => '',
                    'selected' => '1',
    		),
    

    But this does not seem to work.
    Thank you in advance to tell me which Array variable name I should use and how to set it as selected.
    Best Regards,
    Alain

    • This reply was modified 2 years, 5 months ago by alainmelsens.
    • This reply was modified 2 years, 5 months ago by alainmelsens.
    • This reply was modified 2 years, 5 months ago by alainmelsens.
Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘More API PHP scripts examples for updating different form fields’ is closed to new replies.