Plugin Class-bestand:
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 }
Wanneer ajaxurl
echter niet is gedefinieerd, heb ik het gewijzigd ajaxurl
die zich vormt in onderstaande URL
http://example.com/wp-admin/admin-ajax.php?action=get_product_serial_callback
Dit hielp ook niet .
Hoe kan ik de functie get_product_serial_callback
aanroepen om de JSON
-waarden op te halen en deze waarden in function(data)
?
Reacties
Answer
Zet onderstaande code in _construct () functie en verander de actienaam in get_product_serial_callback: –
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" ));
Reacties
- Vraag bijgewerkt. Het roept nog steeds niet eens de functie aan en retourneert 0 als reactie.
- voeg public toe aan functie get_product_serial_callback ().
- Bijv .: publieke functie get_product_serial_callback ()
- WisdmLabs : Hoe krijg ik json-gegevensrespons op de functie
product_serial_ajax
? De ajaxurl die ik als reactie zie ishttp://example.com/wp-admin/admin-ajax.php?action=get_product_serial
- Je hoeft alleen maar add_action uit te voeren met actienaam als ' wp_ajax_get_product_serial ' en roep uw functie aan zoals hierboven gedefinieerd.
Antwoord
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 }
Apart JS-bestand
function load_product(){ jQuery.ajax({ type: "GET", url: ajaxurl, dataType : "JSON", data : {action: "get_product_serial"}, //cache: false, success: function(data){ alert("Eureka"); } }); }
PS: het volgende in de header.php van mijn thema zetten werkte voor mij
<script type="text/javascript"> var ajaxurl = "<?php echo admin_url("admin-ajax.php"); ?>"; </script>
array($this, 'get_product_serial_callback')
moeten zijn, zoals vermeld in de anser.ajaxurl is not defined