Reputation: 9314
I'm getting a little lost :d
Short explanation:
I want to be able to process $_POST data when a submit button is clicked on a custom post type. So firstly how or what function can I use to add a custom input submit, and secondly how can I build a function for the submitting of this button? Can't seem to find a solution anywhere!
Long explanation:
I'm building a authorisation paypal system for a custom post type named orders. I have my orders in a table which I access using a class. I have my payment information and I know what I have to do which is capture the payment, but I want a button to be able to do this and return errors. So I want a separate button to 'update' and a method of capturing the action so I can do my API call etc.
I don't want a plugin. I just need to know how to make an input and then use $_POST.
Can I do it with a custom meta box?
add_meta_box('order_payment','build_order_payment_info','orders','side');
function build_order_payment_info($post){
<input name="ID" type="hidden" value="ID of my payment">
<input name="Other info needed" type="hidden" value="">
<input name="submitPayment" type="submit" value="Process payment">
}
Then in my order meta
add_action('save_post','save_order');
function save_order(){
global$post;
if(isset($_POST['submitPayment'])&&!empty($_POST['ID'])){
//send payment api
}else echo"There was an error with the payment";
}
Would something like that work? I've also read that I can provide the custom post type to the add_action function like this: 'save_post_orders', is that correct?
Upvotes: 2
Views: 4556
Reputation: 9314
Using admin_post
action and wp_nonce_field
.
When using admin_post
it will require a hidden input within the form.
<input name='action' type="hidden" value="add_new_client">
When using this you want to add your action too
add_action('admin_post_add_new_client',array('clients','add_new_client'));
This will result in the form being 'submitted' to clients::add_new_client()
public static function add_new_client(){
//access $post info here but first do some admin checks etc
if(!current_user_can('manage_options'))wp_die('you are not allowed to managed these settings');
check_admin_referer('newClient');
//do your validation and add whatever
}
I've also used wp_nonce_feild
within my form like this
public function buildForm(){
echo"<form name='newclientsForm' action='".admin_url('admin.php?page=clients')."'><table class='form-table'>";
wp_nonce_field('newClient');
//do rest of form
}
Upvotes: 3
Reputation: 378
If I understand you right, you want some input fields and a button that sends the values from the inputs to a file on the server and that file should do something with it?
Well if thats the case, you can do that on 2 ways:
1 - the good old submit button
2 - bind an ajax call to an element
examples:
1: http://www.w3schools.com/tags/att_button_form.asp
2: http://api.jquery.com/jquery.ajax/
well the rest is then just a script on server side (http://php.net/manual/en/reserved.variables.post.php)
note to 2: at the bottom are some examples, they are easier to understand if you've never done this before
Upvotes: 0