Build Forminator custom API.

Would you please provide me with some information or a guide to be able to build Forminator custom API?

  • Adam
    • Support Gorilla

    Hi Lee

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

    I’m aware that Forminator API docs that you’ll find here

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

    might not be very “extensive” but they are not really intended to be a “copy and paste” guide but rather a help for developers – mostly because building add-ons/extensions/integrations based on API is usually quite a complex task so some level of experience is a must.Not really because we don’t want to provide more information (on the contrary, we’re totally open about that) but because it’s just a complicated thing :slight_smile:

    I’ll be more than happy to give you some tips and suggestions on how to proceed but I must honestly admit I’m not quite sure what to begin with so may I first ask you some important questions?

    You mentioned during the chat that you’re not much experienced with that kind of programming so

    – how familiar are you with PHP coding in general? what about OOP?
    – have you ever developed any kind of WordPress plugin (even if it was very simple one)?
    – what about general principles/patterns of WordPress development, concepts of action/filter hooks, how data is stored in WordPress etc?

    – since you mentioned that it would be integration with another service – how are you feeling about things like data format conversions, REST API and similar?

    I’m asking this to get a general idea what to start with and what could I/should I suggest first, I hope that makes sense to you :slight_smile:

    Best regards,
    Adam

  • Lee
    • Freelance WP Developer

    Good morning Adam. Thanks for the reply and offer of assistance. Your email is “juicy” 😊.

    I used to program quite extensively in php in the late 90’s, enough to build custom back-end/front end websites using php and MySQL, but just getting back into it now. Given my lack of experience over the past 20 years or so I’d put myself back at a novice level although not entirely green. I’m currently enrolled in the WPMU Dev WordPress courses offered through the academy and so far flying along.

    As far as your questions go – here you go:

    how familiar are you with PHP coding in general? what about OOP?
    -PHP – I understand basic constructs and know how to read and interpret the codex. OOP – I have no idea what this is 😊
    – have you ever developed any kind of WordPress plugin (even if it was very simple one)?
    I developed custom back-ends using PHP and MySQL, but this was quite a few years ago. I understand variables, arrays, loops etc.

    – what about general principles/patterns of WordPress development, concepts of action/filter hooks, how data is stored in WordPress etc?
    I have a pretty solid understanding of this.

    – since you mentioned that it would be integration with another service – how are you feeling about things like data format conversions, REST API and similar?

    Total NOOB. This is what I need to get a jump on understanding. I have a basic understanding of what an API and Webhooks do, but I fall short on understanding what’s under the hood and how to make them go.

    Overall, my immediate goal is to be able to have data fields from a forminator contact form be automatically sent to my sendinblue account (i.e. name, email, etc.) so don’t have to manually enter them and automate a corresponding workflow. Emulating the integration in Hustle.

    I work well with self research and practice, so any resources you could point me to would be a start – I’m not finding specifically what I need on the web which is a working example of the integration. Any tips and expertise you could give would be a great benefit.

    I’m committed to making it happen, I just need some guidance and to lean on a few experts, so I so appreciate your help!

    Lee

  • Adam
    • Support Gorilla

    Hi Lee

    Thanks for response and all the additional information!

    -PHP – I understand basic constructs and know how to read and interpret the codex. OOP – I have no idea what this is

    So the first part – that’s great, it’s a solid foundation. But the second one can result in some difficulties. “OOP” (and I used the acronym on purpose) stand for “Object Oriented Programming”. It’s quite different to a “good old” functional programming that was pretty much an “essence” of PHP code in late 90’s.

    Most modern plugins and 3rd-party code libraries/examples would nowadays be OOP so it’s important to understand how it works – what are classes and object, how they relate, how to use them and so on.

    – have you ever developed any kind of WordPress plugin (even if it was very simple one)?
    I developed custom back-ends using PHP and MySQL, but this was quite a few years ago. I understand variables, arrays, loops etc.

    – what about general principles/patterns of WordPress development, concepts of action/filter hooks, how data is stored in WordPress etc?
    I have a pretty solid understanding of this.

    All right, that’s a good start!

    – since you mentioned that it would be integration with another service – how are you feeling about things like data format conversions, REST API and similar?

    Total NOOB. This is what I need to get a jump on understanding. I have a basic understanding of what an API and Webhooks do, but I fall short on understanding what’s under the hood and how to make them go.

    That’s actually another crucial thing but, don’t get me wrong, you could probably “get away” with just being able to “apply ready examples” (a bit of like putting lego blocks together) for the start with some simple code – but the more into the woods, the darker it gets and you’ll need to start understand that.

    It’s really a lot to cover, way more than we would be able to assist here I’m afraid. I asked all that to be able to “asses” how much tips/suggestions/additional information you might need but that’s quite a lot, to be honest.

    However, since you do have some experience in PHP coding anyway (even if it was long ago) and do have pretty solid understanding of filter/action hooks in WP and how data is handled in WP – that’s a good start. It might still mean a lot of work and learning but if you’re determined to go thsi way – it could be quite an adventure :slight_smile:

    I’d start with this article first:

    https://wpmudev.com/blog/how-to-build-custom-forminator-add-ons-using-the-developer-api/

    It’s a complete “walk-through” through building a basic Forminator Add-on using its API, along with additional explanation and examples on the way. So it would be best thing to start with and you’d see how far you can get through it and how much sense does it all make to you.

    Then I’d also recommend checking these tutorials here:
    https://www.tutorialspoint.com/php/php_object_oriented.htm

    They might not be fully up to date and are not specifically related to WP but would give you pretty solid “kick-start” into OOP, thus letting you better understand modern code/coding-standards.

    And also this cool article:

    https://www.smashingmagazine.com/2018/01/understanding-using-rest-api/

    to get you started in the entire “API/REST API” area in general (not specifically related to WordPress but principles/terminology are the same) as understanding that is usually necessary to implement any kind of “integration” between two or more unrelated services on the web.

    I hope that helps at least a bit!

    Best regards,
    Adam

  • Lee
    • Freelance WP Developer

    Excellent Response. Thanks Adam.

    Your insights are very helpful in orientating me to the challenge and the resources you provided me should be a good start to begin going down the rabbit hole at least with a safety rope :slight_smile: . I intend on starting the adventure with your “lego building block” approach”. That’s how I best learn. I really appreciate your help and I’ll let you know how I get along.

  • Lee
    • Freelance WP Developer

    Hi again Adam.

    I’m making progress on this I’m happy to report. I found very detailed documentation and developer tools on the sendinblue developer page which has a wealth of information and code examples to utilize their api.

    I have been able to write the required php code to add a contact and associated attributes successfully. My next steps (I think) will be to understand how to hook this php code into the action of the forminator submit action, so when the user submits the form, the variables are passed into the api code and its executed.

    Am I on track? Any guidance on how I should go about doing this?

    Really appreciate your assistance.

    Lee

  • Adam
    • Support Gorilla

    Hi Lee

    I’m glad to hear that you’re making progress!

    Yes, you’re right. If you already can send data to SendInBlue with your code then the next step would be to make form submission “trigger” your code and pass form data to it so you could further process it.

    There are multiple action hooks in Forminator but the ones most relevant would be, I believe, in the “/library/modules/custom-forms/front/front-action.php” file in the plugin.

    I think this one could be a good shot for you:

    do_action( 'forminator_custom_form_submit_before_set_fields', $entry, $form_id, $field_data_array );
    

    Once the form is submitted this will contain form and entry ID and the $field_data_array would contain submitted form data – so you can parse/process that array and re-use data in your code.

    A “pseudo-code” example:

    <?php 
    
    function my_sendinblue_handle( $fields ) {
    // this is your function that sends data to SendInBlue over their API
    // where $fields is an array containing data to be sent
    }
    
    function my_forminator_sendinblue_hook( $entry, $form_id, $form_data ) {
    
    // do something here with $entry, $form_id and $form_data to get them 
    // formatted the way you want/need to work with your code
    
    my_sendinblue_handle( $form_data); // and call your custom code passing form data to it
    
    }
    add_action( 'forminator_custom_form_submit_before_set_fields', 'my_forminator_sendinblue_hook', 10, 3);

    Note please: this isn’t a “ready to use” code but I think it should give you some ideas to move on.

    I’d also suggest testing the hook that I shared to make sure when it’s executed (some of these hooks are only used if form is submitted via Ajax and/or if it’s set to store data in the database) and what’s the format of the data. But in “general terms” – that’s the way to “connect” your custom code to the form submission.

    I hope that helps :slight_smile:

    Best regards,
    Adam

  • Lee
    • Freelance WP Developer

    Thanks Adam!

    Can’t wait to pick this apart and digest it. Excited to give it a try. By the way, the smashingmagazine article you pointed me to did the trick in getting me unstuck and on my way.

    I have to say that WPMUDEV support is absolutely phenomenal. Every experience I have had so far has been uber-positive and encouraging to my development. Thank you and keep up the great work!

    I’ll let you know how I get on.

    Lee

  • Lee
    • Freelance WP Developer

    Hi Adam,
    Just finding the time to get into this now. One question, in the psuedo-code example you have:

    function my_forminator_sendinblue_hook( $entry, $form_id, $form_data ) {

    Should $from_data not be $field_data_array? At this point, I’m just trying to get my function to print out the $form_data_array so I know I am actually dealing with the data, but no luck.

    I will forge on!

    Thanks

  • Lee
    • Freelance WP Developer

    Thanks Adam.

    Me thinks I need to brush up on some basics here.

    I found your previous support post to a similar question here: https://wpmudev.com/forums/topic/forminator-pro-trigger-a-php-function-when-a-forminator-form-is-submitted/ which I am digesting.

    I have to agree with Cedric regarding the level of support! Thanks for taking the time. You’ll most likely hear from me later in the day once I’m stuck in the mire :wink:

  • Lee
    • Freelance WP Developer

    Hi Adam.

    Happy to report success on my integration. I now have Forminator data automatically sent and successfully submitted to my sendinblue account on submit! It took me pretty much all day to figure it out yesterday, and I resisted the urge to reach out to you for help about 100 times :wink:, but promised myself I’d do my best to figure it out on my own (for my own development).

    I still have a couple of issues I’m hoping you could assist with:

    1. The addon action works flawlessly on the “refresh page” submit setting, but it throws an error on the page with the ajax submission option enabled – the standard Forminator submission notice ” An error occurred processing the form. Please try again” ( I could not even get the page to display the $field_data_array during dubugging in this setting). With that said, when using the ajax method the form is still submitted, logged in my DB, emailed notification sent successfully, and the data is submitted to my sendinblue via the API.

    2. I’m wondering if I could send you my hook for you to take a look – there might be some clean up or best practices I’m missing – just for an eyes on evaluation of my code.

    Much appreciated.

    Lee

  • Adam
    • Support Gorilla

    Hi Lee

    Happy to report success on my integration. I now have Forminator data automatically sent and successfully submitted to my sendinblue account on submit!

    Glad to hear that!

    As for your questions, I think it would be best if I could actually see and test your code on my end. Would you share it with me (entire code please – so I could just put it on my site and test)?

    Note please: it won’t probably “go through” this form so it’s better to put e.g. a zip file on Dropbox or Google Drive and just post a link to it here in response.

    Best regards,
    Adam

  • Adam
    • Support Gorilla

    Hi Lee

    Thanks for response!

    I’m sorry for the delay on my end, I wasn’t able to get back to you earlier due to increased traffic.

    Unfortunately, when I tried to download the file now Dropobox statest that it no longer exists. I’m assuming that you have removed it since I wasn’t responding. If you’d like to give it another try, please share the code again and I believe I’ll be able to get back to you much faster this time.

    Best regards,
    Adam

  • Adam
    • Support Gorilla

    Hi Lee

    Thanks for sharing the file, I was able to fetch it and look into it.

    I believe that the culprit here is this part of the code (near the end):

    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    } 

    In case of “reload” type of submission that’s fine as it would eventually just print our response (if any) to the source code but in case of “ajax” method data is processed in a different way. Anything that’s “sent through” from and to any AJAX request should be sent in specific ways – via dedicated error parameters or as a part of JSON response.

    Here you’re not dealing directly with AJAX call so it’s not that important to you but the thing is that just “printing out” any string in a code that’s triggered by the ajax request that expects “ajax response” simply breaks that ajax response. In this case it’s “relatively harmless” due to when/where during the ajax requests the code is executed – so submission is still saved to DB and the CURL is also able to send it to Sendinblue. But in other cases it could also entirely break the code.

    So to sum it up, in general it’d be better to either skip this part all together or instead e.g. log the response to debug log (which will be happening if WP DEBUG is enabled and will be ignored if it’s not) rather than “echo()” it to the page source. Without that code form submits fine both in “reload” mode and in “ajax” mode.

    Best regards,
    Adam

  • Lee
    • Freelance WP Developer

    Cool Beans Adam.

    Tested it out in staging and works like a charm. I really appreciate your detailed explanation, helps me clearly understand what is going on.

    Well, with your help I have successfully finished my quest to build a custom integration with Sendinblue and no one was hurt in the process. :grinning:

    Until next time and thanks again.

    Lee