您需要一秒钟form
的admin_url('admin-post.php')
as作为表单操作。然后,您可以admin_post_custom_action
执行操作。
样例代码:
add_action( 'admin_post_wpse_79898', 'wpse_79898_test' );
function wpse_79898_test() {
if ( isset ( $_GET['test'] ) )
echo esc_html( $_GET['test'] );
die( __FUNCTION__ );
}
在设置页面中:
<form action="<?php echo admin_url( 'admin-post.php' ); ?>">
<input type="hidden" name="action" value="wpse_79898">
<input type="text" name="test" value="">
<?php submit_button( 'Send' ); ?>
</form>
更新资料
这是一个相当扩展的示例。表明:
- 基本安全措施(随机,逃逸),
- 如何注册和使用回调,
- 如何重定向回原始页面,即使是网络激活的插件也可以使用,
- 如何根据允许值的白名单显示自定义消息。
我在此处使用的示例(更新选项)不应仅用于在现场激活的插件。对于网络激活的插件,这非常有用,因为没有针对这些插件的选项API。
我应该添加评论,但是我太懒了。:)我将撰写有关此内容的博客文章,并稍后通过链接更新答案。
<?php
/* Plugin Name: admin-post demo */
add_action( 'wp_loaded', array ( WPSE_79898_Admin_Post_Demo::get_instance(), 'register' ) );
class WPSE_79898_Admin_Post_Demo
{
/**
* Plugin instance.
*
* @see get_instance()
* @type object
*/
protected static $instance = NULL;
protected $action = 'wpse_79898';
protected $option_name = 'wpse_79898';
protected $page_id = NULL;
/**
* Access this plugin’s working instance
*
* @wp-hook wp_loaded
* @return object of this class
*/
public static function get_instance()
{
NULL === self::$instance and self::$instance = new self;
return self::$instance;
}
public function register()
{
add_action( 'admin_menu', array ( $this, 'add_menu' ) );
add_action( "admin_post_$this->action", array ( $this, 'admin_post' ) );
}
public function add_menu()
{
$page_id = add_options_page(
'Admin Post Demo',
'Admin Post Demo',
'manage_options',
'admin-post-demo',
array ( $this, 'render_options_page' )
);
add_action( "load-$page_id", array ( $this, 'parse_message' ) );
}
public function parse_message()
{
if ( ! isset ( $_GET['msg'] ) )
return;
$text = FALSE;
if ( 'updated' === $_GET['msg'] )
$this->msg_text = 'Updated!';
if ( 'deleted' === $_GET['msg'] )
$this->msg_text = 'Deleted!';
if ( $this->msg_text )
add_action( 'admin_notices', array ( $this, 'render_msg' ) );
}
public function render_msg()
{
echo '<div class="' . esc_attr( $_GET['msg'] ) . '"><p>'
. $this->msg_text . '</p></div>';
}
public function render_options_page()
{
$option = esc_attr( stripslashes( get_option( $this->option_name ) ) );
$redirect = urlencode( remove_query_arg( 'msg', $_SERVER['REQUEST_URI'] ) );
$redirect = urlencode( $_SERVER['REQUEST_URI'] );
?><h1><?php echo $GLOBALS['title']; ?></h1>
<form action="<?php echo admin_url( 'admin-post.php' ); ?>" method="POST">
<input type="hidden" name="action" value="<?php echo $this->action; ?>">
<?php wp_nonce_field( $this->action, $this->option_name . '_nonce', FALSE ); ?>
<input type="hidden" name="_wp_http_referer" value="<?php echo $redirect; ?>">
<label for="<?php echo $this->option_name; ?>">Enter some text:</label>
<input type="text" name="<?php echo $this->option_name;
?>" id="<?php echo $this->option_name;
?>" value="<?php echo $option; ?>">
<?php submit_button( 'Send' ); ?>
</form>
<?php
}
public function admin_post()
{
if ( ! wp_verify_nonce( $_POST[ $this->option_name . '_nonce' ], $this->action ) )
die( 'Invalid nonce.' . var_export( $_POST, true ) );
if ( isset ( $_POST[ $this->option_name ] ) )
{
update_option( $this->option_name, $_POST[ $this->option_name ] );
$msg = 'updated';
}
else
{
delete_option( $this->option_name );
$msg = 'deleted';
}
if ( ! isset ( $_POST['_wp_http_referer'] ) )
die( 'Missing target.' );
$url = add_query_arg( 'msg', $msg, urldecode( $_POST['_wp_http_referer'] ) );
wp_safe_redirect( $url );
exit;
}
}