Submitting a form on a custom admin page in wordpress

前端 未结 4 2256
没有蜡笔的小新
没有蜡笔的小新 2021-02-19 21:21

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 proce

4条回答
  •  星月不相逢
    2021-02-19 22:02

    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

    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.

提交回复
热议问题