这当然是可能的,但您可能正在考虑对图像单击执行ajax请求,然后执行短代码并返回输出。这涉及三个不同的步骤,将ajax请求绑定到一个操作,执行ajax请求并处理响应,以及实际编写将处理请求的脚本。此答案不会覆盖所有必要的开销,您需要正确本地化脚本以生成要发布到的正确url,您需要添加检查以确保请求合法,this should not just be copy/pasted.
Handling the click (js)
jQuery(document).ready(function($) {
$(\'#my_image_id\').on(\'click\',function() {
// you need to, at a minimum, include a nonce here as well
var data = {
action: \'process_shortcode_on_image_click\'
}
// change \'/wp-admin/admin-ajax.php to the correct variable generated by wp_localize_script()
$.post(\'/wp-admin/admin-ajax.php\',data).done(function(response) {
// do whatever you want with response, it will contain the shortcode output
// maybe something like $(\'#my_popup_id\').html(response);
});
})
})
Hooking the request onto actions
这可以在函数中进行。php或插件,它们将在每次页面加载时执行。
// these call the appropriate function based on the action passed from the data object in the js
add_action( \'wp_ajax_process_shortcode_on_image_click_action\', \'process_shortcode_on_image_click_ajax\');
add_action( \'wp_ajax_nopriv_process_shortcode_on_image_click_action\', \'process_shortcode_on_image_click_ajax\');
Handling the ajax request
这可以在函数中进行。php或插件,它们将在每次页面加载时执行。
function process_shortcode_on_image_click_ajax() {
// you should check for a nonce and do other validation here to make sure this is a legit request
echo do_shortcode(\'[yourshortcode]\');
die();
}
有关更多信息,请参阅
Handling Ajax in Plugins 从Codex和docs开始
wp_localize_script