Support » Plugin: Forminator - Contact Form, Payment Form & Custom Form Builder » Forminator API – submit form

  • Resolved koulouridis

    (@koulouridis)


    Hello, I’ve searched the forum for custom submit actions with Forminator, but I couldn’t find a solution to my problem.

    The problem at hand is:

    I need, when a user submits a form, to save all the fields’ data to an array, so I can later push the data with wp_remote_post to an external API.

    I’m stuck at how can I check if a user submitted that form with Forminator in PHP and when the user submits it how to store all the fields’ data in an array.

    ALSO – I’m using a snippet plugin to add a snippet of PHP in that specific form, is that the best practice?

    Thank you in advance!

    • This topic was modified 1 month, 1 week ago by koulouridis.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Support Adam – WPMU DEV Support

    (@wpmudev-support8)

    Hi @koulouridis

    I hope you’re well today and thank you for your question!

    I need, when a user submits a form, to save all the fields’ data to an array, so I can later push the data with wp_remote_post to an external API.

    You can use this action hook

    forminator_form_after_save_entry

    For example:

    add_action( 'forminator_form_after_save_entry', 'my_function', 10, 1);
    function my_function( $form_id ) {
    
    // do something here
    // e.g. you can get last entriy (submission) for the form of $form_id ID
    // using Forminator API https://wpmudev.com/docs/api-plugin-development/forminator-api-docs/
    }
    

    ALSO – I’m using a snippet plugin to add a snippet of PHP in that specific form, is that the best practice?

    It’s fine though personally I always prefer to add custom code as MU plugin as it seems safer and less depending on any additional code.

    Best regards,
    Adam

    Thread Starter koulouridis

    (@koulouridis)

    Thank you for quick reply!

    I have a noob question. Here it is:

    Let’s say I want to get the latest submission of that specific $form_id.

    The only thing I’ve found in the documentation was how to get a specific entry for that form (Forminator_API::get_entry function).

    That function does not return the latest entry, but a specific entry.

    How can I modify the function to get the latest only?

    Also, by getting the latest entry, does that mean every time a user clicks submit, the latest entry is the one that he just made?

    Plugin Support Adam – WPMU DEV Support

    (@wpmudev-support8)

    HI @koulouridis

    The “get_entry()” method does return a specific entry and requires both form ID and entry ID but we don’t know the entry ID inside the hook that I suggested.

    The “get_entries()”, however, returns all entries and that would work because you can then just take the one with the highest ID out of it and that’d be your recent/latest one.

    However:

    I was “re-thinking” this and I think it’s not the “best” solution because you would still need to fetch all submissions (which may be resource-intensive) and there is a slight possibility that the “latest entry” fetched this way would belong to other user (if many users submit the form at the same time).

    So alternative approach (and simpler) would be to instead use this action hook:

    forminator_custom_form_submit_before_set_fields

    because it already includes submission data out of the box (so you are sure it’s this specific submission) and spares you unnecessary DB operations (like fetching all entries). Basic example:

    add_action( 'forminator_custom_form_submit_before_set_fields', 'my_function', 10, 3);
    function my_function( $entry, $form_id, $form_data_array ) {
    
    // do something here
    // you got submitted data in $form_data_aray
    // you also have entire "submission" as object in $entry if needed
    
    }

    I think this should work for you better and should be easier to use as $form_data_array is just a standard array “ready to use” in your code.

    Best regards,
    Adam

    Thread Starter koulouridis

    (@koulouridis)

    Thank you for helping me out!

    I’m still a little confused with Forminator functions, I tried using them, and I’m pretty sure I failed to call them correctly.

    To call this function (forminator_custom_form_submit_before_set_fields) what do I need to do?

    Do I just add a shortcode to that function and then paste the shortcode at my page under the form shortcode?

    (I’m using a custom PHP file and I write all the functions there, is that the wrong approach?)

    Also, about clicking the submit button, do I need to add logic for that or does the function know that the user submitted a form and starts the action?

    In case I need to add logic behind clicking the submit button please show me how.

    (I’m interested in two things, trigger an action when submitting a form and getting all the field values of a form)

    Plugin Support Adam – WPMU DEV Support

    (@wpmudev-support8)

    HI @koulouridis

    Thanks for response!

    It’s not a regular function but an action hook.

    To use it, you need to run your own “callback function”. This is exactly the example code that I shared previously:

    add_action( 'forminator_custom_form_submit_before_set_fields', 'my_function', 10, 3);
    function my_function( $entry, $form_id, $form_data_array ) {
    
    // do something here
    // you got submitted data in $form_data_aray
    // you also have entire "submission" as object in $entry if needed
    
    }

    So to start with:

    1. create an empty file with a .php extension (e.g. “my-forminator-custom-code.php”)
    2. put this as first line of that file

    <?php

    3. below it past the code that I shared

    4. upload it to the “/wp-content/mu-plugins” folder of your site’s WordPress install.

    This is a start. At this point whenever user submits form, forminator code will do “forminator_custom_form_submit_before_set_fields” action which, in turn will execute your custom callback function which is “my_function()” function in example above.

    You won’t see anything yet because the “my_function()” doesn’t do anything – there’s no any code inside it.

    To make it do something (for example, as you mentioned in your first post, to “push the data with wp_remote_post to an external API.”) you need to write your own code and put it inside the “my_function()” function.

    So it doesn’t require any changes in form settings (any additional “logic”, shortocdes etc) but inside the “my_function()” function you should put your own PHP code that does “something” with $form_data_array(). It may be saving it file or pushing it to external API – that’s up to you but you need your own custom PHP code in that place.

    Kind regards,
    Adam

    Thread Starter koulouridis

    (@koulouridis)

    Hey Adam!

    Thank you for your patience!

    I’ve created the mu-plugins folder, I created a custom PHP file, I pasted the code you sent me, I also added a print logic.

    The photo below is not from that script but from a plugin that inserts snippets, I tried using both, the forementioned plugin and your way, none of them seem to return the values.

    https://prnt.sc/nof9pcPO0vLb

    What is the mistake here, and I can’t seem to be able to print the field values?

    Plugin Support Adam – WPMU DEV Support

    (@wpmudev-support8)

    Hi @koulouridis

    Thanks for response.

    It won’t print anything like that but you can use WP debugging to see data:

    – enable debugging by adding this lines to the “wp-config.php” file

    define( 'WP_DEBUG', true );
    define( 'WP_DEBUG_LOG', true );
    define( 'WP_DEBUG_DISPLAY', false );

    – then replace this code of yours

    echo '<pre>';
    print_r($form_data_array);
    echo '</pre>';

    with this

    error_log( print_r( $form_data_array, true ) );

    – submit the form and check “debug.log” file on server in “/wp-content/” folder

    You should see the array with data printed out there.

    Note please that this entire code is for further “re-use” of data, like e.g. sending it to external API. Printing it out right on the page would be a different thing and would require more and different code.

    Kind regards
    Adam

    Hi Adam,

    Thanks for the great replies on this thread. You have taught here more wordpress functionalities than I have learnt from YouTube, blogs etc.

    Keep helping with your amazing answers. They are really helpful for new developers like me.

    Thanks

    Thread Starter koulouridis

    (@koulouridis)

    Hello again,

    I’m fairly new to using custom functions, so I guess the fault here is mine.

    Sadly, after extensive testing, I haven’t been able to get this to work…

    Thank you for your patience once again!

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.