WordPress "wp_register_style was called incorrectly"?

I got all the questions answered, but cannot find decent answers with a real solution. So, I'll explain my problem first.

I am using a custom theme I created on the latest version of WordPress.

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

Below are the notifications displayed 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 the file functions.php

:

/**
 * 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 am guessing from the debug notifications that I need to somehow specify for loading WordPress ' wp_enqueue_scripts

, admin_enqueue_scripts

or login_enqueue_scripts

hooks'

+3


source to share


2 answers


Ok I found out what I did 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 that:



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 he added:

add_action( 'wp_enqueue_scripts', 'my_scripts' );

      

+9


source


Fixed deactivation of WC Fields Factory plugin for me.



0


source







All Articles