Call Ajax URL in Plugin

时间:2015-06-18 作者:Slimshadddyyy

Plugin Class File:

function __construct()
    {
        add_shortcode(\'user_registration_form\', array($this, \'shortcode\'));
    }

public function hook(){
        add_action( \'wp_ajax_get_product_serial_callback\', \'get_product_serial_callback\' );
        add_action( \'wp_ajax_nopriv_get_product_serial_callback\', \'get_product_serial_callback\' );
    }

public function product_serial_ajax() { ?>
        <script type="text/javascript">
                jQuery(document).ready(function(){
                    alert(\'Hello World!\');

                    jQuery.ajax({
                                type: \'GET\', 
                                url: "<?php echo admin_url(\'admin-ajax.php\'); ?>",
                                //url: ajaxurl,
                                dataType : "JSON",
                                data : {action: "get_product_serial_callback"},
                                //cache: false, 
                                success: function(data){
                                alert(\'Eureka\')\';
                                }
                            });
                });
        </script><?php
    }

function csv_to_array($filename=\'\', $delimiter=\',\')
    {
            //if(!file_exists($filename) || !is_readable($filename))
            //return FALSE;

            $header = NULL;
            $data = array();
            if (($handle = fopen($filename, \'r\')) !== FALSE)
            {
                    while (($row = fgetcsv($handle, 1024, $delimiter)) !== FALSE)
                    {
                            if(!$header)
                                    $header = $row;
                            else
                                    $data[] = array_combine($header, $row);
                    }
                    fclose($handle);
            }
            return $data;
    }

function get_product_serial_callback(){

        $upload_dir = wp_upload_dir();
        $csvFile = $upload_dir[\'baseurl\'].\'/Eragon-Serial.csv\';
        $csv = $this->csv_to_array($csvFile); //read csv

        foreach ($csv as $serialnum){
                $serial_num_array[]  = $serialnum[\'product_serial\'];
        }

        $json_array = json_encode($serial_num_array);
        echo $json_array;
        die();
    }

function shortcode()
{
    $this->product_serial_ajax();//fetch product serial number
}
但是,当遇到ajaxurl 没有定义,我改了ajaxurl 以下URL中的形式

http://example.com/wp-admin/admin-ajax.php?action=get_product_serial_callback
This 太没用了。

我怎么打电话get_product_serial_callback 函数以获取JSON 值并在中设置这些值function(data) ?

2 个回复
SO网友:Domain

将以下代码放入\\u construct()函数中,并更改操作名称以获取\\u product\\u serial\\u回调:-

add_action( \'wp_ajax_get_product_serial_callback\', array($this,\'get_product_serial_callback\') );
add_action( \'wp_ajax_nopriv_get_product_serial_callback\', array($this,\'get_product_serial_callback\' ));

SO网友:Slimshadddyyy

Plugin Class File:

function __construct()
    {


        add_shortcode(\'user_registration_form\', array($this, \'shortcode\'));
        wp_register_script(\'product-serial\', plugins_url(\'bootstrap/js/product-serial.js\', __FILE__),array(\'jquery\')); //custom jquery for product serial
        wp_enqueue_script( \'product-serial\' ); //custom jquery for product serial

        $this->hook();
    }

    public function hook()
    {
      add_action(\'wp_ajax_get_product_serial\', array( $this,\'get_product_serial\'));
      add_action(\'wp_ajax_nopriv_get_product_serial\',array( $this,\'get_product_serial\') );
    }

    public function product_serial_ajax(){ ?>

        <script type="text/javascript">load_product();</script>

        <?php
    }



   //convert csv data into array
    function csv_to_array($filename=\'\', $delimiter=\',\')
    {
            //if(!file_exists($filename) || !is_readable($filename))
            //return FALSE;

            $header = NULL;
            $data = array();
            if (($handle = fopen($filename, \'r\')) !== FALSE)
            {
                    while (($row = fgetcsv($handle, 1024, $delimiter)) !== FALSE)
                    {
                            if(!$header)
                                    $header = $row;
                            else
                                    $data[] = array_combine($header, $row);
                    }
                    fclose($handle);
            }
            return $data;
    }


    //get product serial number
    function get_product_serial(){


        $upload_dir = wp_upload_dir();
        $csvFile = $upload_dir[\'baseurl\'].\'/Eragon-Serial.csv\';
        $csv = $this->csv_to_array($csvFile); //read csv

        foreach ($csv as $serialnum){
                $serial_num_array[]  = $serialnum[\'product_serial\'];
        }

        $json_array = json_encode($serial_num_array);
        echo $json_array;
        die();
    }

    function shortcode()
    {
      $this->product_serial_ajax(); //fetch product serial number
    }

Seperate JS file

    function load_product(){
         jQuery.ajax({
          type: "GET", 
          url: ajaxurl,
          dataType : "JSON",
          data : {action: "get_product_serial"},
          //cache: false, 
          success: function(data){
            alert(\'Eureka\');
          }
        });
    }

P.S: putting the following in the header.php of my theme worked for me

<script type="text/javascript">
    var ajaxurl = "<?php echo admin_url(\'admin-ajax.php\'); ?>";
    </script>
结束

相关推荐

是否可以使用系统cron来触发与AJAX API挂钩的函数

我有一个真正的cron作业设置,它似乎可以工作。我已经概述了以下基本内容。我问的原因是我没有看到经常提到的这种方法,我想知道这是不是因为这样做是个坏主意?我不想使用wp-cron 和禁用wp-cron 在我的网站上,即使使用真正的cron运行,也会在其他地方引起问题(*/wp-cron.php?doing_wp_cron)Below is an example of what I\'m doing:Functionadd_action(\'wp_ajax_my_function\', \'my_funct