0

I'm creating a custom plugin for WordPress and I'm trying to execute javascript file only on plugin deactivation.

I'm using register_deactivation_hook() and wp_enqueue_script() to execute file only on plugin deactivation.

There are no errors in the code since the javascript code works well if it is called outside the register_deactivation_hook()

This is what I've tried so far:

register_deactivation_hook( __FILE__, 'deactivation' );
function deactivation() {
    function delete_rest_api() {
        wp_enqueue_script('deactivation_data_api', plugins_url('assets/js/deactivation_data_api.js', __FILE__));
    }
    add_action( 'admin_enqueue_scripts', 'delete_rest_api' );
}

In the end, the plugin needs to execute that javascript file only during plugin deactivation.

Thanks in advance.

  • What do you need to do via JavaScript on deactivation? Judging by the file name, you need to interact with an API? If so, you can/should do that with PHP. – Jacob Peattie Jul 30 '19 at 1:04
  • @JacobPeattie Yes, you are right. But complete API is already done with JS and everything work, so, just I need to call that file on the deactivation hook. Do you have any solution for that? Thanks – upss1988 Jul 30 '19 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.