Wordpress 自定义提交按钮
Wordpress custom submit button
我有点迷路了:d
简短说明:
我希望能够在单击自定义 post 类型上的提交按钮时处理 $_POST 数据。那么首先我可以如何或使用什么函数来添加自定义输入提交,其次我如何构建一个函数来提交这个按钮?
似乎无法在任何地方找到解决方案!
详细解释:
我正在为自定义 post 类型命名订单构建授权贝宝系统。我的订单在 table 中,我使用 class 访问它。我有我的付款信息,我知道我必须做的是捕获付款,但我想要一个按钮能够执行此操作并且 return 错误。所以我想要一个单独的按钮 'update' 和一个捕获动作的方法,这样我就可以进行 API 调用等
我不想要插件。我只需要知道如何进行输入然后使用 $_POST。
我可以使用自定义元框吗?
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">
}
然后在我的订单元
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";
}
这样的东西行得通吗?我还读到我可以像这样向 add_action 函数提供自定义 post 类型:'save_post_orders',对吗?
如果我没理解错的话,您需要一些输入字段和一个按钮,用于将输入的值发送到服务器上的文件并且该文件应该对其执行某些操作?
好吧,如果是这样的话,你可以通过两种方式做到这一点:
1 - 好的旧提交按钮
2 - 将 ajax 调用绑定到元素
示例:
1: http://www.w3schools.com/tags/att_button_form.asp
2: http://api.jquery.com/jquery.ajax/
剩下的只是服务器端的脚本 (http://php.net/manual/en/reserved.variables.post.php)
注意 2:底部是一些示例,如果您以前从未这样做过,它们更容易理解
使用 admin_post
操作和 wp_nonce_field
。
使用 admin_post
时,需要在表单中隐藏输入。
<input name='action' type="hidden" value="add_new_client">
使用这个时你也想添加你的动作
add_action('admin_post_add_new_client',array('clients','add_new_client'));
这将导致表格为 'submitted' 到 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
}
我也在我的表单中使用了 wp_nonce_feild
这样的
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
}
我有点迷路了:d
简短说明:
我希望能够在单击自定义 post 类型上的提交按钮时处理 $_POST 数据。那么首先我可以如何或使用什么函数来添加自定义输入提交,其次我如何构建一个函数来提交这个按钮? 似乎无法在任何地方找到解决方案!
详细解释:
我正在为自定义 post 类型命名订单构建授权贝宝系统。我的订单在 table 中,我使用 class 访问它。我有我的付款信息,我知道我必须做的是捕获付款,但我想要一个按钮能够执行此操作并且 return 错误。所以我想要一个单独的按钮 'update' 和一个捕获动作的方法,这样我就可以进行 API 调用等
我不想要插件。我只需要知道如何进行输入然后使用 $_POST。
我可以使用自定义元框吗?
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">
}
然后在我的订单元
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";
}
这样的东西行得通吗?我还读到我可以像这样向 add_action 函数提供自定义 post 类型:'save_post_orders',对吗?
如果我没理解错的话,您需要一些输入字段和一个按钮,用于将输入的值发送到服务器上的文件并且该文件应该对其执行某些操作?
好吧,如果是这样的话,你可以通过两种方式做到这一点:
1 - 好的旧提交按钮
2 - 将 ajax 调用绑定到元素
示例:
1: http://www.w3schools.com/tags/att_button_form.asp
2: http://api.jquery.com/jquery.ajax/
剩下的只是服务器端的脚本 (http://php.net/manual/en/reserved.variables.post.php)
注意 2:底部是一些示例,如果您以前从未这样做过,它们更容易理解
使用 admin_post
操作和 wp_nonce_field
。
使用 admin_post
时,需要在表单中隐藏输入。
<input name='action' type="hidden" value="add_new_client">
使用这个时你也想添加你的动作
add_action('admin_post_add_new_client',array('clients','add_new_client'));
这将导致表格为 'submitted' 到 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
}
我也在我的表单中使用了 wp_nonce_feild
这样的
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
}