The action='' form tag attribute means the form data is sent to the page on which the form resides. If this page is an actual WP “page” post type, it’s template code could be altered to handle to form submission. In fact, the if($_POST['submit']) conditional of your code could also process the form submission before returning the message. But currently it will never execute because return $content; is always encountered first.
If the code required to process the form data does not require WP resources, you can change the action attribute value to lead to a custom .php file which handles the form submission.
If form handling requires WP resources and altering the source page’s code is not an option, either process the form as an Ajax request or route the submission through /wp-admin/admin-post.php. Either of these will require a hidden form field named “action”, whose value is used to create an action hook “handle” or name, to which you add a callback to process the form data and generate a response.
Since you’re new to PHP, you might try the custom .php file approach first before dealing with any WP quirks. Then you’ll know at least the basics are working before adding WP complexity. If you need WP resources, you’ll need to temporarily hard code representative return values from WP function calls to simulate usage of WP resources.
Thread Starter
Abhi raj
(@theonethatstheone)
what if i want the form action to be done on the same post where i have added the shortcode? in that case what would action="" ?
and how do i run a function after the submit button?
Yes, action="" form tag attribute submits to the same page the form is on. You can alter the page template code to handle form submission. Your code is more or less correct except you need to move return $content; to the end of the function declaration so the if($_POST['submit']) can be processed when appropriate.
Of course you would add form data handling code above return "you have clicked submit";