[英]Using a do_shortcode in wordpress via an ajax call
I have the following piece of Ajax which calls a php file which intends to return the HTML content of a shortcode. 我有以下一段Ajax,它调用了一个php文件,该文件旨在返回简码的HTML内容。
The Ajax call looks like this : Ajax调用看起来像这样:
var PostData = "Action=refresh-cart";
jQuery.ajax({
dataType: "text",
type: 'POST',
url : '<?php echo plugins_url( 'class-booking-system/class-booking-process.php', dirname(__FILE__) );?>',
cache: false,
data : PostData,
complete : function() { },
success: function(data) {
// jQuery("#loading-img").hide();
alert(data);
// jQuery("#join-class-div-3").html(data);
}
});
The PHP looks like this : PHP看起来像这样:
<?php
require_once( ABSPATH . '/wp-includes/shortcodes.php' );
if(isset($_POST['Action'])) {
$Action = $_POST['Action'];
if($Action == "refresh-cart") {
echo do_shortcode('[woocommerce_cart]');
}
}
?>
However when I call my Ajax method it returns an HTTP 500 - which I assume means the do_shortcode function was not found in this context. 但是,当我调用Ajax方法时,它将返回HTTP 500-我认为这意味着在此上下文中未找到do_shortcode函数。 How can I give my plugin the ability to call this wordpress function via ajax?
如何使我的插件能够通过Ajax调用此wordpress函数?
I think you should take a look at the Codex article on using Ajax in Plugins . 我认为您应该看看有关在插件中使用Ajax的Codex文章。 It provides a very good example on how to go about making ajax calls in WordPress.
它提供了一个很好的示例,说明如何在WordPress中进行Ajax调用。
Adapting their example to your code I get something like the following: 根据他们的代码修改他们的示例,我得到如下内容:
First we load the javascript. 首先,我们加载javascript。 We also pass some javascript variables via
wp_localize_script
. 我们还通过
wp_localize_script
传递了一些javascript变量。 In this case, we're going to pass the admin's URL for processing all ajax calls. 在这种情况下,我们将传递管理员的URL来处理所有ajax调用。
wp_enqueue_script( 'ajax-script', plugins_url( '/js/my_query.js', __FILE__ ), array('jquery') );
// in JavaScript, object properties are accessed as ajax_object.ajax_url
wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
Second , in our javascript we can make the ajax call and define our ajax "action" and any other data we need in the data object. 其次 ,在我们的javascript中,我们可以进行ajax调用,并定义ajax“操作”以及数据对象中所需的任何其他数据。 Because "action" has kind of a different meaning, I've renamed your action to "refresh_cart".
由于“动作”的含义有所不同,因此我将您的动作重命名为“ refresh_cart”。
jQuery(document).ready(function($) {
$.ajax({
type: 'POST',
url : ajax_object.ajax_url,
cache: false,
data : { 'action': 'my_action', 'refresh_cart': 'yes' },
complete : function() { },
success: function(data) {
// $("#loading-img").hide();
alert(data);
// $("#join-class-div-3").html(data);
}
});
});
Third , we need to set up the callback for our ajax action. 第三 ,我们需要为我们的ajax操作设置回调。
admin-ajax.php
looks through all of WordPress's pre-configured actions and then also looks for anything added to the wp_ajax_$my_action_name
on the back-end and wp_ajax_nopriv_$my_action_name
on the front-end. admin-ajax.php
看起来通过所有的WordPress的预配置的动作,然后还查找添加到任何wp_ajax_$my_action_name
在后端和wp_ajax_nopriv_$my_action_name
的前端。 I am assuming your question concerns the front-end and since in the data
object we set action = my_action
the corresponding action hook would be wp_ajax_nopriv_my_action
... to which we have attached the my_action_callback
function. 我假设您的问题与前端有关,并且由于在
data
对象中我们将action = my_action
设置action = my_action
因此对应的动作钩子应该是wp_ajax_nopriv_my_action
...,并附加了my_action_callback
函数。 WordPress should be fully loaded and their shouldn't be an issue running shortcodes as far as I can tell. 据我所知,WordPress应该已完全加载,运行简短代码不应成为问题。
add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );
function my_action_callback() {
if( isset($_POST['refresh-cart']) && $_POST['refresh-cart'] == 'yes' ) {
echo do_shortcode('[woocommerce_cart]');
}
die();
}
And voila! 瞧! I think that should do it, but I have to warn you that I didn't test any of this, so use with prudence.
我认为应该这样做,但是我必须警告您,我没有测试过任何一个,因此请谨慎使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.