如果您想在用户没有注意到的情况下或多或少地进行处理和重新加载,可以在上执行自定义函数template_redirect
行动
function check_parameter() {
if ( isset( $_GET[\'bar\'] ) && \'foo\' === $_GET[\'bar\'] && is_singular( \'product\' ) ) {
// get remote data e.g. with wp_remote_get( \'http://example.com\' );
// do something
// redirect
global $wp;
wp_redirect( home_url( $wp->request ) ); // this should redirect to the same url without parameters
die;
}
}
add_action( \'template_redirect\', \'check_parameter\' );
如果您想向用户展示正在发生的事情,那么可以使用admin ajax(或wp rest api)。像这样的,
PHP
add_action( \'wp_ajax_my_action\', \'my_action_function\' );
add_action( \'wp_ajax__nopriv_my_action\', \'my_action_function\' ); // for non logged in users, if needed
function my_action_function() {
// check nonce
// check for capabilities, if needed
// check for required $_POST parameters / values
// do something
wp_send_json_error( $data );
// or
wp_send_json_success( $data ); // $data could be the new price for example
}
jQuery(如果愿意,可以使用js)
jQuery.ajax({
method: \'POST\',
url: \'admin-ajax.php\', // use wp_localize_script for this
data: {
action: \'my_action\',
nonce: \'your-nonce-field-value\', // use wp_localize_script for this
bar: \'foo\', // use js/jquery to get url parameter(s)
},
success: function(response){
// do something with response.data
},
});