I am using the WordPress Plugin Boilerplate to show a form to the user via shortcode. I am just a little confused of the flow of data not so much the syntax to do it.

My Main Public Class has a method that creates a form when a user adds the shortcode. I have a class that can take the form data and build a URL that calls an API.

This is how I thought it should work: 1. Javascript function fires when "Submit" is clicked. It checks to make sure the form is complete if so use AJAX to pass the data to my Processesing script. 2. The script builds the URL and calls the API and data should be returned.

Question: Once the data is returned can I pass the data and update the original page with the form to show the data? This is were I am not 100% sure how to do.

So the user experience should include completing the form, submitting (maybe a loader animation) and the data appears below the form.

****I don't need code I am just looking for an direction on the flow of data.**

  • Yes, you can. You can submit arbitrary data via AJAX and update the page after receiving a response from the server. But maybe you should post your code, if you need further help.
    – Sally CJ
    Jun 11 '19 at 0:30
  • 1
    Thank you for confirming and will provide code once i try and get some results on my own. Thanks!
    – Denoteone
    Jun 11 '19 at 5:54

Take a look at the codex for AJAX in Plugins. It provides example of how WordPress expects AJAX requests are made and how to return information back to the request in javascript.

The request gets routed through admin-ajax.php which locates and action in PHP. The rest is up to you.

Note: you must echo/print the content for it to be returned in the body of the response. It's helpful for it to be encoded with wp_json_encode() as well.

  • 1
    This was very helpful. Using the info you provided above and my current code I will take a shot. If I run into issues I will start a new question specifically about the code.
    – Denoteone
    Jun 11 '19 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.