0

I'm trying to enqueue my "scripts.js" located at the root folder of my child theme. I don't understand why the following code (located in "functions.php") doesn't work:

function enqueue_scripts() {
if ( ! is_admin() ) {
    $scriptsrc = bloginfo('stylesheet_directory') . '/scripts.js'; 
        wp_register_script( 'scripts', $scriptsrc );
        wp_enqueue_script( 'scripts' );
    }
}
add_action( 'wp_enqueue_scripts', 'enqueue_scripts' );
1
  • Are other enqueues working? scripts seems a little generic for handle, might something else be colliding with it?
    – Rarst
    Commented Mar 16, 2014 at 14:48

2 Answers 2

1

Technically, your problem (as already pointed out) is that you are using a function that echos instead of returns your path. However, the use of bloginfo/get_bloginfo to retrieve theme directory paths is long since discouraged.

  • 'stylesheet_url' - Displays the primary CSS (usually style.css) file URL of the active theme. Consider echoing get_stylesheet_uri() instead.
  • 'stylesheet_directory' - Displays the stylesheet directory URL of the active theme. (Was a local path in earlier WordPress versions.) Consider echoing get_stylesheet_directory_uri() instead.
  • 'template_url' / 'template_directory' - URL of the active theme's directory ('template_directory' was a local path before 2.6; see get_theme_root() and get_template() for hackish alternatives.) Within child themes, both get_bloginfo('template_url') and get_template() will return the parent theme directory. Consider echoing get_template_directory_uri() instead (for the parent template directory) or get_stylesheet_directory_uri() (for the child template directory).

http://codex.wordpress.org/Function_Reference/bloginfo

As per the Codex section just quoted, the equivalent, correct code should use get_template_directory_uri():

function enqueue_scripts() {
  if ( ! is_admin() ) {
    $scriptsrc = get_template_directory_uri() . '/scripts.js'; 
    wp_register_script( 'scripts', $scriptsrc );
    wp_enqueue_script( 'scripts' );
  }
}
add_action( 'wp_enqueue_scripts', 'enqueue_scripts' );

Note that get_template_directory_uri() will always return a path to the parent theme. If you wish for a child theme to be able to override this file, need get_stylesheet_directory_uri()

Additionally, please do not use a function name as generic as enqueue_scripts. That is likely to cause trouble and is bad practice.

0

The reason your function does not work is because you use bloginfo in variable assignment. The bloginfo function echoes its value, you need to use get_bloginfo which returns it's value.

change:

$scriptsrc = bloginfo('stylesheet_directory') . '/scripts.js';

to:

$scriptsrc = get_bloginfo('stylesheet_directory') . '/scripts.js';

Most WordPress API functions have two versions- the ones with get_ prepended all return their value and should be used when you are assigning that value to a variable or otherwise manipulating the result with php, and not just printing it directly to the page.

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.