Submitting a form on a custom admin page in wordpress

I created a custom page in the wordpress admin which has a simple file upload field and a submit button. I need to figure out how to submit the page to somewhere it can be processed but can’t find anything on the web. Does anyone know what the ‘action’ needs to be on the form to get it to go to a function or another page where i can process the file?

Related posts

Leave a Reply

4 comments

  1. use this code snippet:

    add_action( 'admin_action_wpse10500', 'wpse10500_admin_action' );
    function wpse10500_admin_action()
    {
        // Do your stuff here
        wp_redirect( $_SERVER['HTTP_REFERER'] );
        exit();
    }
    add_action( 'admin_menu', 'wpse10500_admin_menu' );
    function wpse10500_admin_menu()
    {
        add_management_page( 'WPSE 10500 Test page', 'WPSE 10500 Test page', 'administrator', 'wpse10500', 'wpse10500_do_page' );
    }
    function wpse10500_do_page()
    {
    ?>
        <form method="POST" action="<?php echo admin_url( 'admin.php' ); ?>">
            <input type="hidden" name="action" value="wpse10500" />
            <input type="submit" value="Do it!" />
        </form>
    <?php
    } 
    
  2. Thanks Mr. Hunter. Couldn’t credit you for the right answer.

    Wrapped the form in a if(isset($_POST[‘submit’])) and then called my functions.

  3. I am answering this specific part of the OP’s question using a different way than the accepted answer to get results:

    Does anyone know what the ‘action’ needs to be on the form to get it to go to a function…

    post to the same page- via blank form action- different actions can be taken based on the same single form.

    if ( isset($_POST['custom_action']) ){
        if ( $_POST['custom_action'] == 'update'){
            if ( isset($_POST['Submit']) ) {
                //form validation and gather into a single object $valid;
                //I put this in a class, so self::update_main_config($valid);
                //otherwise you can just update_main_config($valid)
            }
            if ( isset($_POST['Submit_Default']) ) {
                //no form validation needed & defaults handled via custom wp_option, this could also be static function very easily
                //secondary action group such as self::restore_config_defaults();
            }
        }
    }
    

    Based on a custom admin page I use for one of my plugins, I conditionally set the context for what I expect to happen. My admin config page submits new values and it can restore default values… but it can also affect other modules based on user input. I am showing how to use fairly simple conditionals to create more complex submit contexts. What if the upload were a photo, and the Admin needs control to post to an arbitrary user profile- but we want to reuse the same code to allow a logged in user to post their own profile pic? This is why contexts, and this is why I think my methodology is relevant.

    I am fond of re-using code and taking advantage of a single functional loop to process any single context- and again, using a single hidden form input, or potentially multiple controls based on user-or-admin events, one can alter the context using the same form and theoretically 1 or more submit buttons. I actually wanted 2 buttons to separate the actions in the mind of the submitter, so I have shown that method.

    Here is the corresponding HTML

    <form name="config_actions" action="" method="post">
    <input type="hidden" id="custom_action" name="custom_action" value="update" />
    <input type="submit" id="my_main_action" name="Submit" value="Update" />
    <input type="submit" id="my_secondary_action" name="Submit_Default" value="Restore Defaults" />
    </form>
    

    Using simple javascript to conditionally set input#custom_action to a value suitable to create my context– for sake of this example it validates when custom_action is set to “update”, although in my code I use other contexts as well based on form responses.

    My prior example gained -2 (!) I use $_POST[‘action’] which means input[name=”action”] not to be confused with form[action=””] as my former code implied- so yeah that was bad. Here I am showing more clearly that I have a hidden input context manager named ‘custom_action’ and I have 2 submit buttons named ‘Submit’ and ‘Submit_Default’ which both effectively trigger the same form because they are both type=submit.