R
R
rkfddf2021-08-16 18:36:58
WordPress
rkfddf, 2021-08-16 18:36:58

How to include styles and scripts in a wordpress child theme?

Styles and scripts are not included in wordpress child theme

add_action( 'wp_enqueue_script', 'my_scripts_slider');

function my_scripts_slider() {
wp_enqueue_script('chifslider1', get_stylesheet_directory_uri() . '/chifslider/chief-slider.dev.js');
wp_enqueue_script('chifslider2', get_stylesheet_directory_uri() . '/chifslider/chief-int.js');      
};


If I insert them into the code without a wrapper
wp_enqueue_script('chifslider1', get_stylesheet_directory_uri() . '/chifslider/chief-slider.dev.js');
then connect, and a message is displayed
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. This notice was triggered by the chifslider1 handle. Please see Debugging in WordPress for more information. (This message was added in version 3.3.0.) in C:\xampp\htdocs\wordpress\wp-includes\functions.php on line 5535

And the same thing happens with styles - without a wrapper it works with laptops, in a wrapper it is not connected at all. That is, the addresses and scripts are correct, but I can’t connect them through the wordpress functionality. How to correctly include styles and scripts in child themes of wordpress?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rkfddf, 2021-08-16
@rkfddf

I already figured it out - there was a typo in several articles, apparently they torn each other. The code from the English version of the codex works without errors.

A
Artem Zolin, 2021-08-16
@artzolin

The hook is called wp_enqueue_scripts, and not wp_enqueue_script
To connect files, there is a great function get_theme_file_uri()that checks for the existence of a file before connecting, it's quite convenient
In the file version, you can specify the time of the last modification, so that when it changes, the browser updates it in its cache withfilemtime()

add_action( 'wp_enqueue_scripts', 'my_scripts_slider' );
function my_scripts_slider() {
  wp_enqueue_script( 'chifslider1', get_theme_file_uri( 'chifslider/chief-slider.dev.js' ), array(), filemtime( get_theme_file_path( '/chifslider/chief-slider.dev.js' ) ) );
  wp_enqueue_script( 'chifslider2', get_theme_file_uri( 'chifslider/chief-int.js' ), array(), filemtime( get_theme_file_path( '/chifslider/chief-int.js' ) ) );
};

Learn more about working with file connection here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question