我不会采用这种方法,而是使用 ajax 来做到这一点。它更容易实现,并且会给你的插件一种非常 web 2.0 的感觉。在您的情况下,实现 ajax 方法实际上更容易,而不是尝试做您正在做的事情。这里有一些东西可以帮助您入门。
<?php
add_action('admin_print_scripts', 'my_action_javascript');
function my_action_javascript() {
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
action: 'my_action',
whatever: 1234
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});
});
</script>
<?php
}
然后你可以像这样处理请求:
<?php
add_action('wp_ajax_my_action', 'my_action_callback');
function my_action_callback() {
global $wpdb; // this is how you get access to the database
$whatever = intval( $_POST['whatever'] );
$whatever += 10;
echo $whatever;
die(); // this is required to return a proper result
}