Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question

1 Answer 1

up vote 0 down vote accepted

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:

function add_my_script() {
    wp_enqueue_script('qtool-insert-v2');
}add_action( 'admin_enqueue_scripts', 'add_my_script' );
share|improve this answer
    
ahh okay thanks for that advice. I am now successfully seeing it in the source. Although their is no redirect or popup happening... also, it looks like this script now loads on every admin page... any way I can only have it triggered on that save_post hook? <script type='text/javascript' src='http://....../plugins/buildStatus2/qtool-insert.js?ver=3.3.2'></script> –  Greg Jul 24 '12 at 15:42
    
looks like I don't need the <script> tags... still need to figure out a way to only have this trigger on a save post tho –  Greg Jul 24 '12 at 15:52
    
You can only trigger the admin_enqueue_scripts on certain admin pages, see the link above (codex.wordpress.org/Plugin_API/Action_Reference/…) –  Rachel Baker Jul 24 '12 at 16:01
    
@Greg Are you trying to run JS on a save post PHP action or a save click event? –  Brian Fegter Jul 24 '12 at 17:02
    
@BrianFegter on a save post action. Whenever a post is published or updated. –  Greg Jul 24 '12 at 17:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.