1

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

2 Answers 2

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' );
1
  • 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. May 21, 2011 at 23:35
0

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

Your Answer

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