So I am working on a plugin and am trying to trigger a js file whenever a post is saved. I have been reading up on this all morning and cannot seem to find why this is not working. Any advice? If I were to paste the js code directly into the plugin it seems to work… I have double checked the path to the js and still no response.
add_action( 'admin_init', 'plugin_admin_init' );
function plugin_admin_init() {
wp_register_script( 'qtool-insert-v2', plugins_url() . '/buildStatus2/' . 'qtool-insert-v2.js' );
}
add_action( 'save_post', 'add_my_script' );
function add_my_script() {
wp_enqueue_script( 'qtool-insert-v2' );
}
Here is the qtool-insert-v2.js – very simple redirect.
<script type="text/javascript">
<!--
window.location = "http://www.google.com/"
//-->
alert("HELLO");
</script>
You need to hook into admin_enqueue_scripts with your add_my_script function.
http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
Example: