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 want to load a .js file only on a specific page in Wordpress 3.1.2 So I use in functions.php

function my_init_method() {
if (is_page('myinfopage')){
   echo "yeeessssssss";
   //wp_register_script('RGraph',get_stylesheet_directory_uri() . '/js/RGraph/RGraph.common.core.js' );
   }
}
add_action('init', 'my_init_method');

The page is 100% 'myinfopage'. I tried it also with the pageID 24 if (is_page(24)){....

But nothing happens. It's not included and noe 'yeeesssss' is displayed. Any idea?

Cheers, Joerg

share|improve this question

migrated from stackoverflow.com May 21 '11 at 23:02

This question came from our site for professional and enthusiast programmers.

2 Answers 2

You probably want to drop the conditional during init. You are just registering the script here, not enqueueing it. Once registered, you should be able to call the script later in the action sequence. Please try something like this:

function myplugin_css() {
    if ( is_page( 'myinfopage' ) ) {
        wp_enqueue_script( 'RGraph' );
    }
}
add_action( 'wp_print_scripts', 'myplugin_css' );
share|improve this answer
    
To add to Michael's answer: the 'init' hook is too soon to use conditionals such as is_page(). You need to use such conditionals late enough in the pageload for WordPress to have defined them. Since wp_print_scripts fires during wp_head, hooking into it is sufficiently late enough for is_page() to have been defined. –  Chip Bennett May 21 '11 at 23:35

add_action just hooks a function call to an event. In order to originally call the action you should use do_action..

Refer http://codex.wordpress.org/Function_Reference/do_action

share|improve this answer

Your Answer

 
discard

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