Title: Handbook Examples: Stylesheets and Scripts
Author: Jose Castaneda
Published: March 31, 2015
Last modified: April 14, 2015

---

 [  ](https://profiles.wordpress.org/jcastaneda/) [Jose Castaneda](https://profiles.wordpress.org/jcastaneda/)
9:26 pm _on_ March 31, 2015      

# Handbook Examples: Stylesheets and Scripts

Let us continue with making some examples, shall we?

This one will focus on scripts and stylesheets.

> No hard coding of scripts, styles and Favicons unless a browser workaround script.
> Everything should be enqueued.

This does include the main stylesheet. I’ll start with a few:

### Example:

All stylesheets **must** be enqueued.

    ```
    add_action( 'wp_enqueue_scripts', 'my_parent_theme_css' );
    function my_parent_theme_css() {
        wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'child-style', get_stylesheet_uri(), array( 'parent-style' ) );
    }
    ```

### Example:

No **hard-coding** of scripts.

    ```
    <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/theme-slider.js"></script>
    <script>
    jQuery( document ).ready( function( $ ){
        $( '.slider' ).fancySlider({
            'autoplay': <?php echo get_theme_mod( 'slider-autoplay', true ); ?>,
            'navStyle': '<?php echo get_theme_mod( 'nav-style', 'circles' ); ?>',
            });
    });
    </script>
    ```

In the functions file:

    ```
    add_action( 'wp_enqueue_scripts', 'my_theme_slider_options' );
    function my_theme_slider_options(){
        wp_enqueue_script( 'theme-slider', get_template_directory_uri() . '/js/theme-slider.js', array( 'jquery' ) );
        wp_enqueue_script( 'theme-slider-init', get_template_directory_uri() . '/js/init.js', array( 'jquery', 'theme-slider' ) );
        // get user options
        $options = array();
        $options['autoplay'] = get_theme_mod( 'slider-autoplay', true );
        $options['navigation_style'] = get_theme_mod( 'nav-style', 'circles' );

        wp_localize_script( 'theme-slider-init', 'themeSliderOptions', $options );
    }
    ```

In the JavaScriptJavaScript JavaScript or JS is an object-oriented computer programming
language commonly used to create interactive effects within web browsers. WordPress
makes extensive use of JS for a better user experience. While PHP is executed on
the server, JS executes within a user’s browser. [https://www.javascript.com](https://www.javascript.com/)
file:

    ```
    jQuery( document ).ready( function( $ ){
        $( '.slider' ).fancySlider({
            'autoplay': themeSliderOptions.autoplay,
            'navStyle': themeSliderOptions.navigation_style,
            });
    });
    ```

So, let’s share more examples and fill that handbook!

### Share this:

 * [  Threads ](https://make.wordpress.org/themes/2015/03/31/handbook-examples-stylesheets-and-scripts/?share=threads)
 * [  Mastodon ](https://make.wordpress.org/themes/2015/03/31/handbook-examples-stylesheets-and-scripts/?share=mastodon)
 * [  Bluesky ](https://make.wordpress.org/themes/2015/03/31/handbook-examples-stylesheets-and-scripts/?share=bluesky)
 * [  Facebook ](https://make.wordpress.org/themes/2015/03/31/handbook-examples-stylesheets-and-scripts/?share=facebook)
 * [  X ](https://make.wordpress.org/themes/2015/03/31/handbook-examples-stylesheets-and-scripts/?share=x)
 * [  LinkedIn ](https://make.wordpress.org/themes/2015/03/31/handbook-examples-stylesheets-and-scripts/?share=linkedin)

 * [Login to Reply](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fmake.wordpress.org%2Fthemes%2F2015%2F03%2F31%2Fhandbook-examples-stylesheets-and-scripts%2F%23respond&locale=en_US)

 1.  ![](https://secure.gravatar.com/avatar/acdecddd34500ea3902e530d670395a027d4173672fc246a82c4f5b421c10a7f?
     s=32&d=mm&r=g)
 2.   [Frumph](https://profiles.wordpress.org/frumph/)  10:27 pm _on_ March 31, 2015
 3.  There’s a reason for hardcoded stylesheet’s to be placed first (ref: get_stylesheet_uri()–
     because you cannot specify placement in the enqueue’ing of scripts and CSSCSS 
     CSS is an acronym for cascading style sheets. This is what controls the design
     or look and feel of a site. is a top down execution order interpreter. Without
     the ability to properly specify what comes first then you will never know if one
     thing is going to override the other.
 4.   * ![](https://secure.gravatar.com/avatar/eae9b03fdf1750ad9ecddf610003712fb406bce09006a0c4596071960057ea0e?
        s=32&d=mm&r=g)
      *  [Ulrich](https://profiles.wordpress.org/grapplerulrich/)  7:22 pm _on_ April
        1, 2015
      * You can define the order of the scripts by defining the priority in which you
        run the action.
      * This will run before most other styles: `add_action( 'wp_enqueue_scripts', '
        my_theme_slider_options', 9 );`
         This will run after most other styles: `add_action('
        wp_enqueue_scripts', 'my_theme_slider_options', 10 );`
 5.  ![](https://secure.gravatar.com/avatar/7ee1212143214d848145a8f11e421057418ec53a7f7aa9fee16986ea8a555d24?
     s=32&d=mm&r=g)
 6.   [ArnaudBan](https://profiles.wordpress.org/arnaudban/)  7:07 am _on_ April 1,
     2015
 7.  The third parameter of wp_enqueue_style() is a dependencies array :
      “Array of
     handles of any stylesheet that this stylesheet depends on; stylesheets that must
     be loaded before this stylesheet. false if there are no dependencies.”
 8.  ![](https://secure.gravatar.com/avatar/8013a62d7397c006bae48b96d6832a573ea71a7d5d3597fe584210567b508c05?
     s=32&d=mm&r=g)
 9.   [Justin Tadlock](https://profiles.wordpress.org/greenshady/)  5:24 pm _on_ April
     1, 2015
 10. The “no hard-coding of scripts” is partially incorrect. This is the part we don’t
     allow:
 11.     ```
         <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/theme-slider.js"></script>
         ```
     
 12. This is perfectly OK:
 13.     ```
         <script>
         jQuery( document ).ready( function( $ ){
             $( '.slider' ).fancySlider({
                 'autoplay': true,
                 'navStyle': 'circles',
                 });
         });
         </script>
         ```
     
 14.  * ![](https://secure.gravatar.com/avatar/a15532e2de67c56ac93ed4e3a26c5b139bf8450f788262910590cd2991b7874c?
        s=32&d=mm&r=g)
      *  [Jose Castaneda](https://profiles.wordpress.org/jcastaneda/)  8:41 am _on_
        April 4, 2015
      * True, I was using a super simplified example. There are times when it requires
        more than, say, 30 lines.
         - ![](https://secure.gravatar.com/avatar/8013a62d7397c006bae48b96d6832a573ea71a7d5d3597fe584210567b508c05?
           s=32&d=mm&r=g)
         -  [Justin Tadlock](https://profiles.wordpress.org/greenshady/)  7:19 pm _on_
           April 5, 2015
         - No problem. I just want to make sure we’re not causing any unnecessary confusion.
            * ![](https://secure.gravatar.com/avatar/27c0751de5ed93222d17118b8e8b3ea1f97be0c79dafaa270e454df903f1afd1?
              s=32&d=mm&r=g)
            *  [kevinhaig](https://profiles.wordpress.org/kevinhaig/)  10:23 pm _on_
              April 14, 2015
            * Why is the top one not allowed and the bottom one allowed?
               - ![](https://secure.gravatar.com/avatar/27c0751de5ed93222d17118b8e8b3ea1f97be0c79dafaa270e454df903f1afd1?
                 s=32&d=mm&r=g)
               -  [kevinhaig](https://profiles.wordpress.org/kevinhaig/)  10:26 pm 
                 _on_ April 14, 2015
               - I thought it also applied to in line scripts (if that is the term)
               - ![](https://secure.gravatar.com/avatar/a15532e2de67c56ac93ed4e3a26c5b139bf8450f788262910590cd2991b7874c?
                 s=32&d=mm&r=g)
               -  [Jose Castaneda](https://profiles.wordpress.org/jcastaneda/)  10:
                 28 pm _on_ April 14, 2015
               - Can remove it via hook.
               - ![](https://secure.gravatar.com/avatar/27c0751de5ed93222d17118b8e8b3ea1f97be0c79dafaa270e454df903f1afd1?
                 s=32&d=mm&r=g)
               -  [kevinhaig](https://profiles.wordpress.org/kevinhaig/)  10:33 pm 
                 _on_ April 14, 2015
               - Yep enqueue’ing is definitely the way to go.
                  I know your slider example
                 is just an example but it would make sense to me to enqueue that script
                 as well because it would allow it to be removed if the user wanted
                 a different slider(as an example).
               - ![](https://secure.gravatar.com/avatar/a15532e2de67c56ac93ed4e3a26c5b139bf8450f788262910590cd2991b7874c?
                 s=32&d=mm&r=g)
               -  [Jose Castaneda](https://profiles.wordpress.org/jcastaneda/)  10:
                 38 pm _on_ April 14, 2015
               - >  it would make sense to me to enqueue that script as well
               - And I wholeheartedly would agree with that. There are times when it
                 makes more sense to hard code because it may be faster. A simple example
                 is Twenty Fifteen:
               -     ```
                     (function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);
                     ```
                 
               - ![](https://secure.gravatar.com/avatar/8013a62d7397c006bae48b96d6832a573ea71a7d5d3597fe584210567b508c05?
                 s=32&d=mm&r=g)
               -  [Justin Tadlock](https://profiles.wordpress.org/greenshady/)  3:38
                 pm _on_ April 15, 2015
               - The top one is not allowed because you must enqueue all script files
                 via `wp_enqueue_script()`. This is so that they can be properly deregistered/
                 dequeued or re-registered.
               - The bottom one is merely some inline code. It should be hooked to `
                 wp_head` or `wp_footer` in most cases. My reply above should’ve mentioned
                 that.
               - You wouldn’t want to add that small bit of code to an additional external
                 file and enqueue it. That’d actually slow page load time with an extra
                 HTTPHTTP HTTP is an acronym for Hyper Text Transfer Protocol. HTTP
                 is the underlying protocol used by the World Wide Web and this protocol
                 defines how messages are formatted and transmitted, and what actions
                 Web servers and browsers should take in response to various commands.
                 request. It’s far better to just add it.
               - What’s not OK with either is just dropping them in `header.php`.
      * ![](https://secure.gravatar.com/avatar/05abba420c13f6ddcefea5f31aedd2fc9f93ee5f511bc631328b0fb7d4b8e87d?
        s=32&d=mm&r=g)
      *  [Jami Gibbs](https://profiles.wordpress.org/jami0821/)  4:45 pm _on_ April
        15, 2015
      * Re: No hard-coding of scripts. What about IE conditional JS? For example:
      * `
          <script src=”/js/html5.js”>
      * `
      * Although maybe this won’t be an issue for long with work being done on JS IE
        conditional comments:
      * [https://core.trac.wordpress.org/ticket/16024](https://core.trac.wordpress.org/ticket/16024)
         - ![](https://secure.gravatar.com/avatar/eae9b03fdf1750ad9ecddf610003712fb406bce09006a0c4596071960057ea0e?
           s=32&d=mm&r=g)
         -  [Ulrich](https://profiles.wordpress.org/grapplerulrich/)  5:27 pm _on_ 
           April 16, 2015
         - It should be added using the
         -     ```
               wp_head
               ```
           
         -  action e.g. [https://gist.github.com/grappler/05568f05633484499acc](https://gist.github.com/grappler/05568f05633484499acc)
 15. ![](https://secure.gravatar.com/avatar/7917c2edce62f57164d6036c47f648628dc1dfdc0d29b3edb2eb305eeb66d440?
     s=32&d=mm&r=g)
 16.  [Mckilem](https://profiles.wordpress.org/mckilem/)  7:42 pm _on_ April 8, 2015
 17. “Don’t include any plugins. A theme can recommend plugins but not include those
     plugins in the theme code.”
 18. Does it mean that I can’t do something like:
 19. And then use something like “TGM PluginPlugin A plugin is a piece of software 
     containing a group of functions that can be added to a WordPress website. They
     can extend functionality or add new features to your WordPress websites. WordPress
     plugins are written in the PHP programming language and integrate seamlessly with
     WordPress. These can be free in the WordPress.org Plugin Directory [https://wordpress.org/plugins/](https://wordpress.org/plugins/)
     or can be cost-based plugin from a third-party. Activation” to recommend PageNavi?
 20.  * ![](https://secure.gravatar.com/avatar/a15532e2de67c56ac93ed4e3a26c5b139bf8450f788262910590cd2991b7874c?
        s=32&d=mm&r=g)
      *  [Jose Castaneda](https://profiles.wordpress.org/jcastaneda/)  7:48 pm _on_
        April 8, 2015
      * Keep in mind this doesn’t really pertain to the topic ( scripts and stylesheets)
        but there are other ways of recommending plugins. TGM is an exception but the
        recommended plugins have to enhance the theme. You wouldn’t recommend a moving
        truck for grocery shopping, right?
         - ![](https://secure.gravatar.com/avatar/7917c2edce62f57164d6036c47f648628dc1dfdc0d29b3edb2eb305eeb66d440?
           s=32&d=mm&r=g)
         -  [Mckilem](https://profiles.wordpress.org/mckilem/)  8:13 pm _on_ April 
           8, 2015
         - I know it doesn’t, but wasn’t able to find a topic where my reply would 
           be more appropriate. Well, can I conclude by your answer that I can add 
           pagenavi, since that would enhance the theme (better pagination)?

### Leave a Reply 󠀁[Cancel reply](https://make.wordpress.org/themes/2015/03/31/handbook-examples-stylesheets-and-scripts/?output_format=md#respond)󠁿

You must be [logged in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fmake.wordpress.org%2Fthemes%2F2015%2F03%2F31%2Fhandbook-examples-stylesheets-and-scripts%2F&locale=en_US)
to post a comment.

This site uses Akismet to reduce spam. [Learn how your comment data is processed.](https://akismet.com/privacy/)

# Post navigation

[← Weekly meeting agenda This week we’ve got our…](https://make.wordpress.org/themes/2015/03/30/weekly-meeting-agenda-this-week-weve-got-our/)

[Theme review team weekly notes The logs are… →](https://make.wordpress.org/themes/2015/04/07/theme-review-team-weekly-notes-the-logs-are-10/)