3

How do you add a script on the footer of the post editor ? I'm adding a meta box under the post editor and I need to include a javascript before the closing body tag (on the footer)

How do I achieve this ?

The script is not needed on the front-end just on that meta_box (just on the post editor page)

1 Answer 1

5

Hook into 'admin_footer-post-new.php' and 'admin_footer-post.php', check the global variable $post_type:

add_action( 'admin_footer-post-new.php', 'wpse_73692_script' );
add_action( 'admin_footer-post.php', 'wpse_73692_script' );

function wpse_73692_script()
{
    if ( 'post' !== $GLOBALS['post_type'] )
        return;

    ?>
<script>alert( '<?php echo $GLOBALS['post_type'];?>' );</script>
    <?php
}
4
  • Where would you add this on a multi-site installation where you just want the script to fire in just one particular site?
    – Lynn
    Commented Jan 11, 2013 at 20:17
  • @Lynn In a plugin. Themes should handle the front-end only.
    – fuxia
    Commented Jan 11, 2013 at 20:21
  • Ugh. For reasons i can't go into nor care to debate, I can't do this via a plugin. Or install a new one (new company policy right now). And out of interest in learning WP, I want to know if I could just put this into the functions.php file relevant to the one site I want my jQuery to fire on (when the Publish button is clicked) and have it hook into admin footer from there.
    – Lynn
    Commented Jan 11, 2013 at 20:27
  • @Lynn Then do it so. Try it, it should work.
    – fuxia
    Commented Jan 11, 2013 at 20:33

Your Answer

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

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