0

Why am I getting this error?

Notice: wp_register_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in /var/www/domain.com/wp-includes/functions.php on line 3792

The code I'm using to include my custom CSS and JS files is:

// Register a custom style and script.
wp_register_style( 'custom_css', get_stylesheet_directory_uri() . '/custom.css' );
wp_register_script( 'custom_js', get_stylesheet_directory_uri() . '/custom.js', array('jquery') );

// Enqueue a custom style and script in pages and posts.
function add_custom_scripts() {
    wp_enqueue_style( 'custom_css' );
    wp_enqueue_script( 'custom_js' );
}
add_action( 'wp_enqueue_scripts', 'add_custom_scripts', 9999 );

Edit: Per Pieter's answer, I changed my code to this and it fixed the problem:

function add_custom_scripts() {
    // Start "Add Custom Scripts."
    wp_register_style( 'custom_css', get_stylesheet_directory_uri() . '/custom.css' );
    wp_register_script( 'custom_js', get_stylesheet_directory_uri() . '/custom.js', array('jquery') );

    wp_enqueue_style( 'custom_css' );
    wp_enqueue_script( 'custom_js' );
}
add_action( 'wp_enqueue_scripts', 'add_custom_scripts', 9999 );

function add_custom_scripts_login() {
    // Start "Add Custom Scripts."
    wp_register_style( 'custom_css', get_stylesheet_directory_uri() . '/custom.css' );
    wp_register_script( 'custom_js', get_stylesheet_directory_uri() . '/custom.js', array('jquery') );

    wp_enqueue_style( 'custom_css' );
    wp_enqueue_script( 'custom_js' );
}
add_action( 'login_enqueue_scripts', 'add_custom_scripts_login', 9999 );
1

1 Answer 1

1

The notice is quite obvious. It is telling you that you are not running wp_register_style() within the wp_enqueue_scripts hook.

SOLUTION

Move your wp_register_script() and wp_register_style() calls to inside your function which is hooked to wp_enqueue_scripts

3
  • Shouldn't he move wp_register_style and wp_register_script inside the function rather than wp_enqueue_script and wp_enqueue_style since they are already in the function? Feb 18, 2016 at 6:58
  • denis is right, but thanks anyway, Pieter, for the lead.
    – GTS Joe
    Feb 18, 2016 at 7:00
  • @GTSJoe jip, sorry, silly mistake. But you get the drift. Glad it helped, enjoy ;-) Feb 18, 2016 at 7:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.