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
}
However, when encountered ajaxurl
is not defined, I changed it ajaxurl
which forms in below URL
http://example.com/wp-admin/admin-ajax.php?action=get_product_serial_callback
This too did not helped.
How can I call get_product_serial_callback
function in order to fetch the JSON
values and set those values in function(data)
?
array($this, 'get_product_serial_callback')
as mentioned in the anser.ajaxurl is not defined