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.

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?

share|improve this question
    
when in the request process are you calling that function? –  Milo Mar 20 '12 at 3:55
    
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? –  Chip Bennett Mar 20 '12 at 15:02
add comment

1 Answer

up vote 4 down vote accepted

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 )
        return;
    // On public pages, choose your conditions from the list of Conditional Tags
    // Example: Only on pages
    if ( ! is_page() )
        return;
    // ...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' );

EDIT

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:
            {$some_var}
        </script>";
}

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

share|improve this answer
    
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 Mar 20 '12 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 Mar 20 '12 at 14:54
add comment

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.