[Forminator Pro] Trigger a php function when a forminator form is submitted

I'm looking to trigger a PHP function from a house made plugin i'm building when a user submit a form in forminator. (I'd like to avoid building a custom wp form in my plugin)

I heard there may be a forminator API to do so but the documentation only shows how to call an already built third-party integration.
Is there an API doc I'm missing out?

  • Adam Czajczyk
    • Support Gorilla

    Hi Cedric

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

    There's a Forminator API doc here:

    https://premium.wpmudev.org/docs/wpmu-dev-plugins/forminator-api-docs/

    However, in this case you'd rather want to just hook to one of the action hooks (that are not documented there). Assuming that you want to trigger your code upon form submission (as in "form", not "quiz" or "poll" though there are hooks for those as well), the

    forminator_custom_form_after_handle_submit

    would probably be the best.

    If you look into the "/wp-content/plugins/forminator/library/modules/custom-forms/front/front-action.php" file you'll find this:

    /**
     * Action called after full form submit
     *
     * @since 1.0.2
     *
     * @param int   $form_id  - the form id
     * @param array $response - the post response
     */
    do_action( 'forminator_custom_form_after_handle_submit', $form_id, $response );

    So to "hook up" to that you'd want to do something like this in your own code:

    add_action( 'forminator_custom_form_after_handl_submit', 'my_callback_function' );
    function my_callback_function( $form_id, $response) {
    
    /* first check if $response['success']
    and if so grab $form_id and continue
    with your code */
    }

    You "my_callback_function()" would be triggered after form is submitted so you would want to first check if it was successful (see the code in the referenced file right below the quoted "do_action()" code to see how it's handled there) and if it's fine, then take $form_id and use the API (as referenced under the Forminator API docs link above) to do whatever you need to do with the form data.

    I hope that helps but if you have any additional questions, let me know, please.

    Best regards,
    Adam

    • Cedric
      • Flash Drive

      Hey Adam,

      Thank you for your response. Every time I received an email saying that I got a reply from you I know I'm in for a treat! :smiley:

      An action hook was exactly what I was hoping for. I'll look into it right away!

      Thanks again!
      Cedric

      • Adam Czajczyk
        • Support Gorilla

        Hi Cedric

        Thank you for your kind words, I really appreciate that!

        Just a note though: I just noticed that there's a small type in my post above in hook name. It shouldn't be:

        forminator_custom_form_after_handl_submit

        but

        forminator_custom_form_after_handle_submit

        I thought that's worth clearing up in case you'd copy some code and was wondering why it doesn't work - I know from experience that such small "typos" are often the most difficult to "track down" :smiley:

        Have a nice day!
        Adam

        • Cedric
          • Flash Drive

          Hey Adam,

          So I really need to freshen my WordPress development skills but for some reason, I'm not able to hook my plugin function to the forminator_custom_form_after_handle_submit event.

          I added a register_action function to my homemade plugin and it is successfully called. However, my cs_job_added_action isn't called after submitting a form. Can you see something messed up? (I'd really like to do it inside my homemade plugin)

          `public function register_action() {
          add_action( 'forminator_custom_form_after_handle_submit', array( $this, 'cs_job_added_action'));
          //is called
          //throw new Exception(' register_action cs_job_added_action');
          }

          public function cs_job_added_action( $form_id, $response) {
          //never called :slight_frown:
          throw new Exception('test cs_job_added_action');
          }`

  • Adam Czajczyk
    • Support Gorilla

    Hi Cedric

    I'm wondering in that case whether your callback function ("cs_job_added_action") does even "exist" when it's necessary. Do you think you could share a bit more of your custom code with me? Perhaps entire plugin maybe? I have marked this ticket as internal in case you'd rather keep the code private but it could help if I was able to get a "whole picture" :slight_smile:

    Best regards,
    Adam

  • Adam Czajczyk
    • Support Gorilla

    Hi Cedric

    Thanks for the code!

    I checked it and did some testing and there are two things that needs to be changed. One of them is actually something that I should have tested before.

    I have suggested the "forminator_custom_form_after_handle_submit" action hook but... I didn't test it, I only based my response on plugin's code. However, seeing that it's not working for you, I checked it and it seems it's not actually called when I would expect it.

    There are two other hooks that can be used instead and they do work (I confirmed that with both my simple test code and your plugin):

    forminator_custom_form_before_save_entry

    and

    forminator_custom_form_after_save_entry

    They both work and will give you the form ID (but there's no $response parameter there!) and the difference is only that the first one is called before and second one after the submission is saved to the database.

    So, the first change to be done would be to replace this line:

    add_action( 'forminator_custom_form_after_handle_submit', array( $this, 'cs_job_added_action'));

    with this one

    add_action( 'forminator_custom_form_after_save_entry', array( $this, 'cs_job_added_action'));

    Alternatively, you may use that second ("after") hook.

    The second change is a consequence of this one - you'll need to adjust your callback function so this line

    public function cs_job_added_action( $form_id, $response) {

    becomes this

    public function cs_job_added_action( $form_id ) {

    The final change, and that's related to the plugin bootstrap code that you're using, is to actually move the "add_action" line from "register_action" (which is for actually registering actions to let other code hook to your plugin rather than to hook your plugin to other code - if I understand that correcty) to the _contruct function. This way:

    public function __construct( $plugin_name, $version ) {
    
    		$this->plugin_name = $plugin_name;
    		$this->version = $version;
    			add_action( 'forminator_custom_form_after_save_entry', array( $this, 'cs_job_added_action'));
    
    	}

    This should do the trick. I tested it on my own setup (your plugin with these changes), just writing the $from_id to the debug.log upon submission and it work like a charm. The callback is triggered and it gets $form_id so you can further process form data then using Forminator API.

    Best regards,
    Adam

  • Cedric
    • Flash Drive

    Hey Adam,

    I'm astonished by the level of support!
    Thank you so much for taking the time to test it so thoroughly!

    I was eager to implement it but for some reason, the callback still doesn't trigger my function.
    When you'll get some times could you take a look at my updated file and tell me if something is different from your implementation? (I already took so much of your time that I'm in no hurry)
    https://owncloud.cedricleruth.com/owncloud/index.php/s/VkGeioxDGm4hIVX

    In my log below, I can see the construct registering the callback but it's not triggered after submitting my form.

    [16-Apr-2019 15:54:48 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:54:50 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:54:52 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:54:53 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:54:53 UTC] PHP Warning: Cannot modify header information - headers already sent in /var/web/site/public_html/wp-content/plugins/membership-pro/app/controller/class-ms-controller-plugin.php on line 99
    [16-Apr-2019 15:55:03 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:55:05 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:55:05 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:55:05 UTC] PHP Warning: Cannot modify header information - headers already sent in /var/web/site/public_html/wp-content/plugins/membership-pro/app/controller/class-ms-controller-plugin.php on line 99
    [16-Apr-2019 15:55:17 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:55:17 UTC] PHP Warning: trim() expects parameter 1 to be string, array given in /var/web/site/public_html/wp-includes/class-wp-user.php on line 206
    [16-Apr-2019 15:55:18 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:55:18 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:55:18 UTC] PHP Warning: Cannot modify header information - headers already sent in /var/web/site/public_html/wp-content/plugins/membership-pro/app/controller/class-ms-controller-plugin.php on line 99
    [16-Apr-2019 15:55:52 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:56:50 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:56:50 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:56:50 UTC] PHP Warning: Cannot modify header information - headers already sent in /var/web/site/public_html/wp-content/plugins/membership-pro/app/controller/class-ms-controller-plugin.php on line 99

    [16-Apr-2019 15:57:52 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:58:53 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:58:53 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 15:58:53 UTC] PHP Warning: Cannot modify header information - headers already sent in /var/web/site/public_html/wp-content/plugins/membership-pro/app/controller/class-ms-controller-plugin.php on line 99
    [16-Apr-2019 15:59:52 UTC] __construct forminator_custom_form_after_save_entry
    [16-Apr-2019 16:00:54 UTC] __construct forminator_custom_form_after_save_entry

  • Adam Czajczyk
    • Support Gorilla

    Hi Cedric

    I just replaced the class-cs_dash-public.php file in your plugin (the one that I downloaded from here previously) with your new file and tested it on my end with the Forminator form and it works perfectly fine but you need first to remove this line

    throw new Exception('test cs_job_added_action' );

    from your "cs_js_added_action" callback.

    Try doing that and it should work fine. However, there's one more "catch" in it that I actually forgot about as it's quite rarely used setting: it cannot have Data Storage disabled (the "Disable store submissions in my database" option in form "Settings"). If you db storage of submission is disabled none of these two hooks will ever be triggered, thus your callback will also never be executed.

    To sum it up:
    - remove aforementioned line for the code
    - make sure that "Disable store submissions in my database" option is not active for the form that you're testing that with

    and give it another try, please. Let's see if it works then. I believe it should :slight_smile:

    Best regards,
    Adam

  • Cedric
    • Flash Drive

    Hey Adam,

    I just found out the issue while playing around with my form setting.
    In my case, it doesn't work when the setting Behaviour > Method is set to "page reload".
    Just changed it to "Ajax" and I'm able to print the ID in the error_log.

    This has been a very informative thread.
    Thank you once more and if there is anything I can do to show my gratefulness let me know!

    Also if you want to remove the internal flag on the thread if it can help others I'm ok with that.

    See you around,
    Cédric

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.