Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress "wp_register_style was called incorrectly"?

Tags:

php

wordpress

I've google'd all over the place for an answer, but can't find any decent answers with an actual solution. So, I'll firstly explain my issue.

I'm using a custom theme that I've made, in the latest version of WordPress.

I wanted to do the proper thing, and not hardcode my styles and scripts into the header.php file, but enqueue them using WordPress functions.

Here are the notices shown when debugging is enabled:

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 C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

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 C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

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 C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_register_script 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 C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_register_script 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 C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_enqueue_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 C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_enqueue_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 C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_enqueue_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 C:\xampp\htdocs\my-project-new\wp-includes\functions.php on line 3560

Notice: wp_enqueue_script 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 C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Notice: wp_enqueue_script 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 C:\xampp\htdocs\my-project\wp-includes\functions.php on line 3560

Here's the relevant part in my functions.php file:

/**
 * Register global styles & scripts.
 */
wp_register_style('my-fonts', '//fonts.googleapis.com/css?family=Lato:300,400,700,900');
wp_register_style('fontawesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css');
wp_register_style('my-styles', get_template_directory_uri() . '/assets/css/main.css');

wp_register_script('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array( 'jquery' ));
wp_register_script('scripts', get_template_directory_uri() . '/assets/js/scripts.js', array( 'jquery' ));


/**
 * Enqueue global styles & scripts.
 */

wp_enqueue_style('my-styles');
wp_enqueue_style('my-fonts');
wp_enqueue_style('fontawesome');

wp_enqueue_script('bootstrap');
wp_enqueue_script('scripts');

I'm guessing from the debugging notices that I need to somehow specify to load WordPress's 'wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks'

like image 214
Daniel Dewhurst Avatar asked Jul 27 '15 09:07

Daniel Dewhurst


1 Answers

Well, I found out what I'd done wrong! Here's what I did for anyone interested:

I changed this:

/**
 * Register global styles & scripts.
 */
wp_register_style('my-fonts', '//fonts.googleapis.com/css?family=Lato:300,400,700,900');
wp_register_style('fontawesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css');
wp_register_style('my-styles', get_template_directory_uri() . '/assets/css/main.css');

wp_register_script('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array( 'jquery' ));
wp_register_script('scripts', get_template_directory_uri() . '/assets/js/scripts.js', array( 'jquery' ));


/**
 * Enqueue global styles & scripts.
 */

wp_enqueue_style('my-styles');
wp_enqueue_style('my-fonts');
wp_enqueue_style('fontawesome');

wp_enqueue_script('bootstrap');
wp_enqueue_script('scripts');

To this:

function my_scripts() {
/**
 * Register global styles & scripts.
 */
wp_register_style('my-fonts', '//fonts.googleapis.com/css?family=Lato:300,400,700,900');
wp_register_style('fontawesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css');
wp_register_style('my-styles', get_template_directory_uri() . '/assets/css/main.css');

wp_register_script('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array( 'jquery' ));
wp_register_script('scripts', get_template_directory_uri() . '/assets/js/scripts.js', array( 'jquery' ));


/**
 * Enqueue global styles & scripts.
 */

wp_enqueue_style('my-styles');
wp_enqueue_style('my-fonts');
wp_enqueue_style('fontawesome');

wp_enqueue_script('bootstrap');
wp_enqueue_script('scripts');
}

And added this:

add_action( 'wp_enqueue_scripts', 'my_scripts' );
like image 196
Daniel Dewhurst Avatar answered Nov 15 '22 03:11

Daniel Dewhurst