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?
Leave a Reply
You must be logged in to post a comment.
use this code snippet:
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.
I had to change code like this to work:
I am answering this specific part of the OP’s question using a different way than the accepted answer to get results:
post to the same page- via blank form action- different actions can be taken based on the same single form.
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.