Support » Plugins » Handling POST Requests

  • Resolved hever



    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:

       = "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

Viewing 3 replies - 1 through 3 (of 3 total)
  • In other words:
    Does anybody know how Handling POST and AJAX Requests in wordpress could work ?

    I solved the problem now.

    The problem after the above approach was, that I could handle my own request data but other data like a changed post was not saved.

    I had to add global $wp_db_version; to avoid the database upgrade error and I had to restore the original action, to let post.php know what it should do:

    function MyMetaData_update()
        // avoid database upgrade error
        global $wp_db_version;
        // restore orinial action
        global $action;
        $action = $_REQUEST["originalaction"];
        // let post.php handle other parts of the request
        require_once(WP_PLUGIN_DIR . '/../../wp-admin/post.php');

    This works but I don’t think its a good reference solution. I still think this feature was never tested, because you have to hack a lot araound. Handling POST data could be much easier. An action could be perhaps called by post.php and not just admin-post.php. Then you need not to change the form action via JS and it would be not needed to include the post.php script and providing some workarounds.

    Current solution:

    1. Redirect form action to admin-post.php using JavaScript
    2. Add a hidden input field with your action. (I don’t think this would work with multiple actions, but I did not test it.)
    3. Handle the action using add_action and a function
    4. Provide a workaround in your function and include post.php

    That Codex page did more bad than good.

    You should just do:

    add_action('save_post', 'my_callback', 10, 2);
    function my_callback($post_id, $post) {
      // get data from $_POST;
      // etc.

    Even easier: use the Custom Field Template plugin.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Handling POST Requests’ is closed to new replies.