最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp enqueue script - WordPress | enqueue_scripts in a child's theme returns error

programmeradmin0浏览0评论

I intended to load a script within a child's theme because I wanted to experiment with a feature. Initially, I loaded the script as follows:

// loading menu toggle function
function menu_toggle_enqueue_script() {
    wp_enqueue_script( 'menu-toggle', get_template_directory_uri().'/assets/js/menu.js');
}
add_action('wp_enqueue_scripts', 'menu_toggle_enqueue_script');

Unfortunately, that returned an error.

I intended to load a script within a child's theme because I wanted to experiment with a feature. Initially, I loaded the script as follows:

// loading menu toggle function
function menu_toggle_enqueue_script() {
    wp_enqueue_script( 'menu-toggle', get_template_directory_uri().'/assets/js/menu.js');
}
add_action('wp_enqueue_scripts', 'menu_toggle_enqueue_script');

Unfortunately, that returned an error.

Share Improve this question edited May 19, 2020 at 15:39 joubibdev asked May 19, 2020 at 15:34 joubibdevjoubibdev 11 bronze badge 1
  • What error? Please be as specific as possible. – fuxia Commented May 21, 2020 at 9:09
Add a comment  | 

1 Answer 1

Reset to default -1

The reason being that:

get_template_directory_uri() actually returns the parent's theme url.

To fix it, I simply concatenated the remainder of the path. In this case, the code became:

// loading menu toggle function | located in the child's theme folder
function menu_toggle_enqueue_script() {
    wp_enqueue_script( 'menu-toggle', get_template_directory_uri(). '-child' . '/assets/js/menu.js');
}
add_action('wp_enqueue_scripts', 'menu_toggle_enqueue_script');

I figured out the solution on my own and thought I would share it since I did not find this solution anywhere I researched.

发布评论

评论列表(0)

  1. 暂无评论