Child Theme Path Being Ignored With wp_enqueue_scripts
I've created a child theme under wp-content/themes/my-theme
with 5 files thus far:
|-my-theme
|----assets
|--------css
|------------bootstrap.min.css
|--------js
|------------bootstrap.min.js
|----functions.php
|----index.php
|----style.css
Now the problem is within my functions.php
file, I've tried adding my .css
and .js
file like this:
<?php
# functions.php
/**
* add css | js files to WP
*/
function theme_asset_setup()
{
# styles
wp_enqueue_style('bootstrap', get_template_directory_uri(). '/assets/css/bootstrap.min.css', );
# scripts
wp_enqueue_script('bootstrap', get_template_directory_uri(). '/assets/js/bootstrap.min.js', );
}
/** actions **/
add_action('wp_enqueue_scripts', 'theme_asset_setup');
This half does the job. This adds my .js
and .css
file with this path:
http://site.local/wp-content/themes/twentynineteen/assets/css/bootstrap.min.css?ver=5.1
My parent theme is twentynineteen
as specified in my style.css
file:
/*
Theme Name: My Theme
Author: treybake
Description: foobar
Requires at Least: WordPress 4.9.6
Version: 0.1
License: GNU General Public License v2 or later
Text Domain: my-theme
Template: twentynineteen
*/
So I'm guessing this has some sort of factor on why my .css
and .js
- but I'm not sure how to debug further.
Why is my child theme path being ignored for my css/js files?
Thanks
child-theme
add a comment |
I've created a child theme under wp-content/themes/my-theme
with 5 files thus far:
|-my-theme
|----assets
|--------css
|------------bootstrap.min.css
|--------js
|------------bootstrap.min.js
|----functions.php
|----index.php
|----style.css
Now the problem is within my functions.php
file, I've tried adding my .css
and .js
file like this:
<?php
# functions.php
/**
* add css | js files to WP
*/
function theme_asset_setup()
{
# styles
wp_enqueue_style('bootstrap', get_template_directory_uri(). '/assets/css/bootstrap.min.css', );
# scripts
wp_enqueue_script('bootstrap', get_template_directory_uri(). '/assets/js/bootstrap.min.js', );
}
/** actions **/
add_action('wp_enqueue_scripts', 'theme_asset_setup');
This half does the job. This adds my .js
and .css
file with this path:
http://site.local/wp-content/themes/twentynineteen/assets/css/bootstrap.min.css?ver=5.1
My parent theme is twentynineteen
as specified in my style.css
file:
/*
Theme Name: My Theme
Author: treybake
Description: foobar
Requires at Least: WordPress 4.9.6
Version: 0.1
License: GNU General Public License v2 or later
Text Domain: my-theme
Template: twentynineteen
*/
So I'm guessing this has some sort of factor on why my .css
and .js
- but I'm not sure how to debug further.
Why is my child theme path being ignored for my css/js files?
Thanks
child-theme
add a comment |
I've created a child theme under wp-content/themes/my-theme
with 5 files thus far:
|-my-theme
|----assets
|--------css
|------------bootstrap.min.css
|--------js
|------------bootstrap.min.js
|----functions.php
|----index.php
|----style.css
Now the problem is within my functions.php
file, I've tried adding my .css
and .js
file like this:
<?php
# functions.php
/**
* add css | js files to WP
*/
function theme_asset_setup()
{
# styles
wp_enqueue_style('bootstrap', get_template_directory_uri(). '/assets/css/bootstrap.min.css', );
# scripts
wp_enqueue_script('bootstrap', get_template_directory_uri(). '/assets/js/bootstrap.min.js', );
}
/** actions **/
add_action('wp_enqueue_scripts', 'theme_asset_setup');
This half does the job. This adds my .js
and .css
file with this path:
http://site.local/wp-content/themes/twentynineteen/assets/css/bootstrap.min.css?ver=5.1
My parent theme is twentynineteen
as specified in my style.css
file:
/*
Theme Name: My Theme
Author: treybake
Description: foobar
Requires at Least: WordPress 4.9.6
Version: 0.1
License: GNU General Public License v2 or later
Text Domain: my-theme
Template: twentynineteen
*/
So I'm guessing this has some sort of factor on why my .css
and .js
- but I'm not sure how to debug further.
Why is my child theme path being ignored for my css/js files?
Thanks
child-theme
I've created a child theme under wp-content/themes/my-theme
with 5 files thus far:
|-my-theme
|----assets
|--------css
|------------bootstrap.min.css
|--------js
|------------bootstrap.min.js
|----functions.php
|----index.php
|----style.css
Now the problem is within my functions.php
file, I've tried adding my .css
and .js
file like this:
<?php
# functions.php
/**
* add css | js files to WP
*/
function theme_asset_setup()
{
# styles
wp_enqueue_style('bootstrap', get_template_directory_uri(). '/assets/css/bootstrap.min.css', );
# scripts
wp_enqueue_script('bootstrap', get_template_directory_uri(). '/assets/js/bootstrap.min.js', );
}
/** actions **/
add_action('wp_enqueue_scripts', 'theme_asset_setup');
This half does the job. This adds my .js
and .css
file with this path:
http://site.local/wp-content/themes/twentynineteen/assets/css/bootstrap.min.css?ver=5.1
My parent theme is twentynineteen
as specified in my style.css
file:
/*
Theme Name: My Theme
Author: treybake
Description: foobar
Requires at Least: WordPress 4.9.6
Version: 0.1
License: GNU General Public License v2 or later
Text Domain: my-theme
Template: twentynineteen
*/
So I'm guessing this has some sort of factor on why my .css
and .js
- but I'm not sure how to debug further.
Why is my child theme path being ignored for my css/js files?
Thanks
child-theme
child-theme
asked 6 hours ago
treyBaketreyBake
1176
1176
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
get_template_directory_uri()
is explicitly for getting the URL to the parent theme, which is why your scripts and styles are being enqueued at that path (in this context the "template" is the parent theme).
The equivalent function for getting the child theme path is get_stylesheet_directory_uri()
. If you don't have a child theme then both these functions do the same thing, but when you are using a child theme the choice is important.
However, both these functions have been superseded by much more useful functions: get_theme_file_uri()
and get_parent_theme_file_uri()
.
get_theme_file_uri()
will get the URL to a specific file in your theme. If your theme is a child theme it will look for the file there, but if it can't find it, it will look in the parent theme. get_stylesheet_directory_uri()
can't do this.
So for your use case, you should use get_theme_file_uri()
:
wp_enqueue_style('bootstrap', get_theme_file_uri( 'assets/css/bootstrap.min.css' ), );
wp_enqueue_script('bootstrap', get_theme_file_uri( 'assets/js/bootstrap.min.js' ), );
The main difference in usage is that rather than concatenating the rest of the file path to the end, you pass it as an argument. This is why it's able to check the parent theme for the file.
ah I see! Seems a bit backwards to not get applied theme stylesheet directory rather than get parent theme uri .. but hey ho!! will accept in 5 mins
– treyBake
6 hours ago
add a comment |
Because this is a child theme, you should use
# styles
wp_enqueue_style('bootstrap', get_stylesheet_directory_uri(). '/assets/css/bootstrap.min.css', );
# scripts
wp_enqueue_script('bootstrap', get_stylesheet_directory_uri(). '/assets/js/bootstrap.min.js', );
get_template_directory_uri()
pulls from the parent theme directory
get_stylesheet_directory_uri()
pulls from the child theme directory
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "110"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f331341%2fchild-theme-path-being-ignored-with-wp-enqueue-scripts%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
get_template_directory_uri()
is explicitly for getting the URL to the parent theme, which is why your scripts and styles are being enqueued at that path (in this context the "template" is the parent theme).
The equivalent function for getting the child theme path is get_stylesheet_directory_uri()
. If you don't have a child theme then both these functions do the same thing, but when you are using a child theme the choice is important.
However, both these functions have been superseded by much more useful functions: get_theme_file_uri()
and get_parent_theme_file_uri()
.
get_theme_file_uri()
will get the URL to a specific file in your theme. If your theme is a child theme it will look for the file there, but if it can't find it, it will look in the parent theme. get_stylesheet_directory_uri()
can't do this.
So for your use case, you should use get_theme_file_uri()
:
wp_enqueue_style('bootstrap', get_theme_file_uri( 'assets/css/bootstrap.min.css' ), );
wp_enqueue_script('bootstrap', get_theme_file_uri( 'assets/js/bootstrap.min.js' ), );
The main difference in usage is that rather than concatenating the rest of the file path to the end, you pass it as an argument. This is why it's able to check the parent theme for the file.
ah I see! Seems a bit backwards to not get applied theme stylesheet directory rather than get parent theme uri .. but hey ho!! will accept in 5 mins
– treyBake
6 hours ago
add a comment |
get_template_directory_uri()
is explicitly for getting the URL to the parent theme, which is why your scripts and styles are being enqueued at that path (in this context the "template" is the parent theme).
The equivalent function for getting the child theme path is get_stylesheet_directory_uri()
. If you don't have a child theme then both these functions do the same thing, but when you are using a child theme the choice is important.
However, both these functions have been superseded by much more useful functions: get_theme_file_uri()
and get_parent_theme_file_uri()
.
get_theme_file_uri()
will get the URL to a specific file in your theme. If your theme is a child theme it will look for the file there, but if it can't find it, it will look in the parent theme. get_stylesheet_directory_uri()
can't do this.
So for your use case, you should use get_theme_file_uri()
:
wp_enqueue_style('bootstrap', get_theme_file_uri( 'assets/css/bootstrap.min.css' ), );
wp_enqueue_script('bootstrap', get_theme_file_uri( 'assets/js/bootstrap.min.js' ), );
The main difference in usage is that rather than concatenating the rest of the file path to the end, you pass it as an argument. This is why it's able to check the parent theme for the file.
ah I see! Seems a bit backwards to not get applied theme stylesheet directory rather than get parent theme uri .. but hey ho!! will accept in 5 mins
– treyBake
6 hours ago
add a comment |
get_template_directory_uri()
is explicitly for getting the URL to the parent theme, which is why your scripts and styles are being enqueued at that path (in this context the "template" is the parent theme).
The equivalent function for getting the child theme path is get_stylesheet_directory_uri()
. If you don't have a child theme then both these functions do the same thing, but when you are using a child theme the choice is important.
However, both these functions have been superseded by much more useful functions: get_theme_file_uri()
and get_parent_theme_file_uri()
.
get_theme_file_uri()
will get the URL to a specific file in your theme. If your theme is a child theme it will look for the file there, but if it can't find it, it will look in the parent theme. get_stylesheet_directory_uri()
can't do this.
So for your use case, you should use get_theme_file_uri()
:
wp_enqueue_style('bootstrap', get_theme_file_uri( 'assets/css/bootstrap.min.css' ), );
wp_enqueue_script('bootstrap', get_theme_file_uri( 'assets/js/bootstrap.min.js' ), );
The main difference in usage is that rather than concatenating the rest of the file path to the end, you pass it as an argument. This is why it's able to check the parent theme for the file.
get_template_directory_uri()
is explicitly for getting the URL to the parent theme, which is why your scripts and styles are being enqueued at that path (in this context the "template" is the parent theme).
The equivalent function for getting the child theme path is get_stylesheet_directory_uri()
. If you don't have a child theme then both these functions do the same thing, but when you are using a child theme the choice is important.
However, both these functions have been superseded by much more useful functions: get_theme_file_uri()
and get_parent_theme_file_uri()
.
get_theme_file_uri()
will get the URL to a specific file in your theme. If your theme is a child theme it will look for the file there, but if it can't find it, it will look in the parent theme. get_stylesheet_directory_uri()
can't do this.
So for your use case, you should use get_theme_file_uri()
:
wp_enqueue_style('bootstrap', get_theme_file_uri( 'assets/css/bootstrap.min.css' ), );
wp_enqueue_script('bootstrap', get_theme_file_uri( 'assets/js/bootstrap.min.js' ), );
The main difference in usage is that rather than concatenating the rest of the file path to the end, you pass it as an argument. This is why it's able to check the parent theme for the file.
edited 6 hours ago
answered 6 hours ago
Jacob PeattieJacob Peattie
17.3k42031
17.3k42031
ah I see! Seems a bit backwards to not get applied theme stylesheet directory rather than get parent theme uri .. but hey ho!! will accept in 5 mins
– treyBake
6 hours ago
add a comment |
ah I see! Seems a bit backwards to not get applied theme stylesheet directory rather than get parent theme uri .. but hey ho!! will accept in 5 mins
– treyBake
6 hours ago
ah I see! Seems a bit backwards to not get applied theme stylesheet directory rather than get parent theme uri .. but hey ho!! will accept in 5 mins
– treyBake
6 hours ago
ah I see! Seems a bit backwards to not get applied theme stylesheet directory rather than get parent theme uri .. but hey ho!! will accept in 5 mins
– treyBake
6 hours ago
add a comment |
Because this is a child theme, you should use
# styles
wp_enqueue_style('bootstrap', get_stylesheet_directory_uri(). '/assets/css/bootstrap.min.css', );
# scripts
wp_enqueue_script('bootstrap', get_stylesheet_directory_uri(). '/assets/js/bootstrap.min.js', );
get_template_directory_uri()
pulls from the parent theme directory
get_stylesheet_directory_uri()
pulls from the child theme directory
add a comment |
Because this is a child theme, you should use
# styles
wp_enqueue_style('bootstrap', get_stylesheet_directory_uri(). '/assets/css/bootstrap.min.css', );
# scripts
wp_enqueue_script('bootstrap', get_stylesheet_directory_uri(). '/assets/js/bootstrap.min.js', );
get_template_directory_uri()
pulls from the parent theme directory
get_stylesheet_directory_uri()
pulls from the child theme directory
add a comment |
Because this is a child theme, you should use
# styles
wp_enqueue_style('bootstrap', get_stylesheet_directory_uri(). '/assets/css/bootstrap.min.css', );
# scripts
wp_enqueue_script('bootstrap', get_stylesheet_directory_uri(). '/assets/js/bootstrap.min.js', );
get_template_directory_uri()
pulls from the parent theme directory
get_stylesheet_directory_uri()
pulls from the child theme directory
Because this is a child theme, you should use
# styles
wp_enqueue_style('bootstrap', get_stylesheet_directory_uri(). '/assets/css/bootstrap.min.css', );
# scripts
wp_enqueue_script('bootstrap', get_stylesheet_directory_uri(). '/assets/js/bootstrap.min.js', );
get_template_directory_uri()
pulls from the parent theme directory
get_stylesheet_directory_uri()
pulls from the child theme directory
answered 6 hours ago
rudtekrudtek
3,55921434
3,55921434
add a comment |
add a comment |
Thanks for contributing an answer to WordPress Development Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f331341%2fchild-theme-path-being-ignored-with-wp-enqueue-scripts%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown