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
1 Answer
Reset to default -1The 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.