I'm trying to modify plugin script adding email notification after a function was executed but I always get an error: "Uncaught ReferenceError: ajax_send_notification_object is not defined". Sorry guys, I'm just new to wordpress and it's my first question here. I'm not sure if it's because the JS file is in the plugins folder. Thanks in advance.
Here's my code in functions.php:
function ajax_send_notification_init(){
wp_register_script('ajax-send-notification-script', plugins_url() . '/plugin-name/js/the-plugin.js', array('jquery') );
wp_localize_script( 'ajax-send-notification-script', 'ajax_send_notification_object', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));
}
add_action('init', 'ajax_send_notification_init');
function ajax_send_notification(){
echo json_encode(array("message" => "test"));
exit;
}
add_action( 'wp_ajax_ajax_send_notification', 'ajax_send_notification' );
add_action( 'wp_ajax_nopriv_ajax_send_notification', 'ajax_send_notification' );
Here's my inserted code in js file:
function sendNotification(){
$.ajax({
type: 'POST',
dataType: 'json',
url: ajax_send_notification_object.ajaxurl,
data: {
'action': 'ajax_send_notification'
},
success: function(data){
alert(data.messsage);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
}