Handling POST Requests
-
Hello,
I’m writing a simple plugin that replaces the standard custom fields box at the post edit backend with a box for more specific and predefined meta data. In my box are some simple input fields.
In this case I’m not going to submit the data using AJAX (what is really easy and working). I’m going to submit the data with a standard request if the user hits the Enter key or clicks on “update Post”.
The WordPress documentation says:
If your plugin or theme needs to handle a POST or AJAX request, direct the request to admin-post.php or admin-ajax.php, respectively.
My question is how should I do this?
I can’t use a new form, because it would be form in form and I want that the other data like post changes are not lost.My not working approach is:
I used JS to change the action of the existing form to redirect the request to admin-post.php:jQuery(document).ready( function() { document.post.action = "admin-post.php"; } );
To get this working I must also add a hidden field with my action:
<input type="hidden" name="action" value="MyMetaData_update" />
And if I handle the action my function is called:
add_action('admin_post_MyMetaData_update', 'MyMetaData_update'); function MyMetaData_update() { // this function is called require_once(WP_PLUGIN_DIR . '/../../wp-admin/post.php'); }
The function is called and I can handle my own fields, but the others like a changed post or other things are not handled. The script stops with an empty screen.
To handle the other changes I tried to include post.php with no success. I get a database upgrade error each time.
The WordPress documentation don’t really describe how to redirect the request and I don’t think changing the form action via JS is the correct solution. So it would be nice if someone could tell me what I should do or try.
Currently I’m not sure if this feature/trigger was ever tested…
Thanks in advance
- The topic ‘Handling POST Requests’ is closed to new replies.