I am trying to add some js to the head using add_action('wp_head', 'js_func'); which works when posted just after the function, however I do not want it posted on every page, just the page that it's relevant to and I am trying to do it like so

function things_to_do_on_this_page() {
    add_action('wp_head', 'js_func');
    //other functiony things

However when it is called like so the js isn't placed at all, thus breaking the page.

Is it possible to do this?

  • when in the request process are you calling that function?
    – Milo
    Commented Mar 20, 2012 at 3:55
  • 1
    It would probably be more useful if you reworded your question to ask how to hook into an action conditionally. Also: what are the conditions under which you want to enqueue this script? Commented Mar 20, 2012 at 15:02

1 Answer 1


If you want to find out whether you're on a specific page, post, in a category archive, etc., then core has to offer Conditional Tags for this.

If you want to hook a js-script definition, then a better way to do this is the following:

// Assuming that you drop your js code into a separate file instead of just between <script>-tags

// 1st: Register during init hook
function add_my_script()
    wp_register_script( $name, $url, $dependency, filemtime( $path ), true )
add_action( 'init', 'add_my_script' );

// 2nd: Enqueue during enqueue hook
function enqueue_my_script()
    // Choose your conditions:
    // On Admin, check against "global $hook_suffix, $pagenow, $typenow"
    if ( 'post-new.php' !== $typenow )
    // On public pages, choose your conditions from the list of Conditional Tags
    // Example: Only on pages
    if ( ! is_page() )
    // ...Nothing to do on login

    // print the script to the screen
    wp_enqueue_script( $name );
// Choose where you need it
add_action( 'wp_enqueue_scripts', 'enqueue_my_script' );
add_action( 'admin_enqueue_scripts', 'enqueue_my_script' );
add_action( 'login_enqueue_scripts', 'enqueue_my_script' );


You can sum up/collect all your little functions, that get hooked somewhere and put the add_action calls into a function that gets loaded on the first hook that's available to themes: after_setup_theme. Important is that the child themes functions.php gets loaded first, then the parent themes and then the after_setup_theme hook. So you can let the possibility open to change things from inside your parent or child theme.

function theme_bootstrap_fn() 
    add_action( 'wp_head', 'js_func' );
    // other functiony things
add_action( 'after_setup_theme', 'theme_bootstrap_fn' );

// Define your function:
function js_func()
    echo "
        <script type='text/javascript>
            // do stuff
            // Add variables like this:

But to answer your question: Yes, you can call every global function inside another function.

  • I have the script, it's just a "loose" bit of code that I want to put on to show and hide divs based on a check box so I don't need to enqueue all the scripts, just place it on the page when that particular function is called.
    – Joshua
    Commented Mar 20, 2012 at 5:48
  • @Joshua You will maybe later add more js, so imho it's never a bad idea. If you're using plugins like Cachify, then the files get collected and bundled into a single one, so it doesn't matter. But: See edit.
    – kaiser
    Commented Mar 20, 2012 at 14:54

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.