-1

I'm doing a plugin that needs to be able to post data to a third party API. Users will fill out a form and press submit.

The form itself - would it be "correct" to simply append html to a page content with a the_content() filter? I'm adding the form based on a bunch of settings saves as meta for each page or post. I'd like to avoid tampering with templates...

With other words; if some meta is set for the page - append some html (the form that users will input data to).

Also - when users press "submit" in that form - how should I handle the request? Right now I'm thinking that I will load wordpress in a totally separate php-file (in order to get my hand on all those funky wp-functions and constants) - but that feels kind of wrong...

Is there a build-in way to both display the form and then handle the request?

Thank you very much!

  • Nothing really - asking to know what the best practice is - before I do anything wrong. – Daniel Setréus Nov 28 '13 at 13:55
  • It strikes me that I could probably post the form to the same page and in my plugin look for any POST data - and handle it that way. However - what if I want to send the form using jQuery? Is there any way to tell WP not to print anything / stop all output to screen - from within my plugin? – Daniel Setréus Nov 28 '13 at 13:59
  • Best practice on WordPress Development is to show your research effort and possibly some code that isn't working. I'm certain you'll find enough information on here to get started and come back with the above mentioned. – Nicolai Nov 28 '13 at 14:06
  • Maybe you could point me in the right direction for best practice? Or perhaps - if you don't have an answer - not reply at all? I have been looking for an answer - without finding anything that suits my needs. I think an answer here might help some other developers in the future. But ok, I can give you some code... although - as I said - I'm not asking for precise code - but rather a hint on what path to go... – Daniel Setréus Nov 28 '13 at 14:53
0

Ok - I finally found a way to do this - and I believe it can be seen as a best practice...

I wanted to post with jQuery from a form on the WP front end. Was a lot easier than I expected.

First of all, make sure you have javascript to capture the form event (when a user presses the submit button). Post the form to the wordpress file admin-ajax.php - found using:

admin_url('admin-ajax.php')

And - this is important - make sure you add a parameter called "action" in the post request. The value of "action" will be used to name the wp-function you will use as callback (or form handler).

Let's say you give the action parameter a value of "my_callback". You would then create the two actions:

add_action( 'wp_ajax_my_callback', 'my_callback_function' );
add_action( 'wp_ajax_nopriv_my_callback', 'my_callback_function' );

Then - simply create the function my_callback_function() - and you are free to use any $_POST data in it.

What you echo out in that function will be returned to the initial jQuery request

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.