Child Theme Path Being Ignored With wp_enqueue_scripts












1















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










share|improve this question



























    1















    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










    share|improve this question

























      1












      1








      1








      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










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 6 hours ago









      treyBaketreyBake

      1176




      1176






















          2 Answers
          2






          active

          oldest

          votes


















          3














          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.






          share|improve this answer


























          • 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



















          1














          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






          share|improve this answer























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            3














            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.






            share|improve this answer


























            • 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
















            3














            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.






            share|improve this answer


























            • 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














            3












            3








            3







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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



















            • 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













            1














            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






            share|improve this answer




























              1














              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






              share|improve this answer


























                1












                1








                1







                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






                share|improve this answer













                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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 6 hours ago









                rudtekrudtek

                3,55921434




                3,55921434






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Loup dans la culture

                    How to solve the problem of ntp “Unable to contact time server” from KDE?

                    ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux