Add Page in Plugin
I'm writing my first Wordpress plugin and need to add a page that does processing of a form. I have a shortcode that adds a form to any page. That form needs to post to a URL where I can process the data (save to DB and send email).
I initially added a process.php file in my plugin directory and posted to that, which works to get the posted data, but I have no access to any wordpress features (which I need to access the database table I created).
Ideally I would like to have a url like /plugin-name/process that I can use. I'm assuming there has to be a way to have that direct to a function in my main plugin code file, but I can't seem to find how to do that.
I'm a complete noob with Wordpress (primar开发者_运维技巧ily a .NET developer, but with PHP experience), so any help would be appreciated.
Few things are not clear, but as per my understanding I can suggest below solution :
use wordpress 'init' action to catch posted data, for example
add_action('init', 'ur_form_process_fun');
function ur_form_process_fun(){
if(isset($_POST['unique_hidden_field'])) {
// process form data here
}
}
In above code ur_form_process_fun() function trigger at initializing stage of wordpress, and your form's action should be default site url action="<?php site_url()?>"
, so that, submitted data will be posted to base url of site and it can be available to init
action.
unique_hidden_field
can be unique hidden input field of your form, this is just to confirm that data is coming from your form.
Hope this may solve your problem. :)
You could use the add_feed function to create the action path page, write something similar to that in your plugin functions.php or anywhere you define you wordpress actions:
add_action('init', function () {
add_feed('/plugin/form_id_action', function(){
include 'your-action-file.php');
})
});
In your-action-file.php you should be able to use wordpress functions as usual.
<form action="/pluging/form_id_action" id="form_id_action">
...
</form>
Refs: https://codex.wordpress.org/Rewrite_API/add_feed https://www.pmg.com/blog/a-mostly-complete-guide-to-the-wordpress-rewrite-api/
精彩评论