通过Ajax调用使用do_short code字中的preSS [英] Using a do_shortcode in wordpress via an ajax call

查看:177
本文介绍了通过Ajax调用使用do_short code字中的preSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面这段阿贾克斯这就要求它打算返回短code中的HTML内容的PHP文件。

I have the following piece of Ajax which calls a php file which intends to return the HTML content of a shortcode.

Ajax调用看起来是这样的:

The Ajax call looks like this :

 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);

                        }           
                });

在PHP看起来是这样的:

The PHP looks like this :

<?php
require_once( ABSPATH . '/wp-includes/shortcodes.php' );

if(isset($_POST['Action'])) {
        $Action = $_POST['Action'];
        if($Action == "refresh-cart") {


           echo do_shortcode('[woocommerce_cart]'); 

            }
        }
?>

然而,当我把我的Ajax方法返回一个HTTP 500 - 我假设意味着do_short code函数没有在这方面找到。我怎样才能把我的插件通过AJAX调用这个词preSS功能的能力?

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. How can I give my plugin the ability to call this wordpress function via ajax?

推荐答案

我觉得你应该看看在codeX文章使用的阿贾克斯插件。它提供了如何去制作Ajax调用在Word preSS一个很好的例子。

I think you should take a look at the Codex article on using Ajax in Plugins. It provides a very good example on how to go about making ajax calls in WordPress.

适应他们为榜样,以您的code我得到类似如下:

Adapting their example to your code I get something like the following:

首先我们加载的JavaScript。我们也可以通过 wp_localize_script 通过一些JavaScript变量。在这种情况下,我们要通过管理员的URL处理所有Ajax调用。

First we load the javascript. We also pass some javascript variables via wp_localize_script. In this case, we're going to pass the admin's URL for processing all ajax calls.

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' ) ) );

,在我们的JavaScript,我们可以使Ajax调用和定义我们的AJAX行动,我们需要在数据对象中的任何其它数据。因为行动有一种不同的含义,我已经改名为你的行动,refresh_cart。

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. Because "action" has kind of a different meaning, I've renamed your action to "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);
        }
    });

});

第三,我们需要建立回调我们的阿贾克斯行动。 管理​​-ajax.php 通过查找所有Word preSS的pre-配置的动作,然后还找什么添加到 wp_ajax_ $ my_action_name 上的前端后端和 wp_ajax_nopriv_ $ my_action_name 。我假设你的问题涉及前端和因为在数据对象,我们设置行动= my_action 相应的动作钩将 wp_ajax_nopriv_my_action ...这是我们已附上 my_action_callback 功能。字preSS应该是满载和他们不应该不多了codeS一个问题,据我可以告诉。

Third, we need to set up the callback for our ajax action. 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. 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. WordPress should be fully loaded and their shouldn't be an issue running shortcodes as far as I can tell.

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.

这篇关于通过Ajax调用使用do_short code字中的preSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆