0

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

    });            
}

1 Answer 1

1

You have registered your JS code in wrong hooks . Always CSS and JS should be registered using wp_enqueue_scripts. Also you have only registered your JS code. You have not enqueued your JS file using wp_enqueue_script( 'ajax-send-notification-script' );

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' )
    ));
    // To enqueque the JS file.
    wp_enqueue_script( 'ajax-send-notification-script' );

}

add_action('wp_enqueue_scripts', 'ajax_send_notification_init');
1
  • Hi, Thanks for answering. I tried your answer but I still get the "Uncaught ReferenceError: ajax_send_notification_object is not defined" and when I view page source I noticed that the script was added twice. I think the plugin already enqueued the script? It's actually an already built script that I'm only modifying. Is it possible to use this kind of ajax even the script is in the plugins folder, cos when I tried before in a script inside my child theme it is working. Thanks again Commented Aug 5, 2016 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.