• I am trying to learn how to create my own theme by following the theme handbook but have come to a bit of a crossroad and need some clarification. In the handbook it states that:

    The proper way to add scripts and styles to your theme is to enqueue them in the functions.php files.

    So based on the functions.php file I created from a previous theme handbook page I added the enqueue scripts like this:

    /* Lets enqueue the css and js files */
    	
    	wp_enqueue_style( 'boilerplate', get_template_directory_uri() . '/css/boilerplate.css',false,'1.0','all');
    	wp_enqueue_style( 'style', get_template_directory_uri() . '/css/style.css',false,'1.0','all');
    	wp_enqueue_script( 'respond.min', get_template_directory_uri() . '/js/respond.min.js', array ( 'jquery' ), 1.0, true);

    Now reading further down the page I see reference to this:

    It is best to combine all enqueued scripts and styles into a single function, and then call them using the wp_enqueue_scripts action. This function and action should be located somewhere below the initial setup (performed above).

    The example given is this:

    function add_theme_scripts() {
      wp_enqueue_style( 'style', get_stylesheet_uri() );
     
      wp_enqueue_style( 'slider', get_template_directory_uri() . '/css/slider.css', array(), '1.1', 'all');
     
      wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);
     
        if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
          wp_enqueue_script( 'comment-reply' );
        }
    }
    add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );

    Here is what I have done inside my functions.php:

    <?php
    /**
     * simply_basic's functions and definitions
     *
     * @package simply_basic
     * @since simply_basic 1.0
     */
     
    /**
     * First, let's set the maximum content width based on the theme's design and stylesheet.
     * This will limit the width of all uploaded images and embeds.
     */
    if ( ! isset( $content_width ) )
        $content_width = 980; /* pixels */
     
    if ( ! function_exists( 'simply_basic_setup' ) ) :
    /**
     * Sets up theme defaults and registers support for various WordPress features.
     *
     * Note that this function is hooked into the after_setup_theme hook, which runs
     * before the init hook. The init hook is too late for some features, such as indicating
     * support post thumbnails.
     */
    function simply_basic_setup() {
     
        /* Lets enqueue the css and js files */
    	
    	wp_enqueue_style( 'boilerplate', get_template_directory_uri() . '/css/boilerplate.css',false,'1.0','all');
    	wp_enqueue_style( 'style', get_template_directory_uri() . '/css/style.css',false,'1.0','all');
    	wp_enqueue_script( 'respond.min', get_template_directory_uri() . '/js/respond.min.js', array ( 'jquery' ), 1.0, true);
    	
    	/**
         * Make theme available for translation.
         * Translations can be placed in the /languages/ directory.
         */
        load_theme_textdomain( 'simply_basic', get_template_directory() . '/languages' );

    Should I be using the first example or the second one to enqueue my scripts? This is a little confusing for me. Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Ummm, both? Mainly the second, but if need be, the first could also be included inside the add_theme_scripts() function. All function calls to enqueue styles and scripts need to be inside a callback function hooked to ‘wp_enqueue_scripts’ action. At least for front end scripts. There are other actions for admin and login page scripts.

    While one could make several callback functions instead of one, there’s little point because they will all be called in sequence at the same time. It’s rather disorganized to do so. Better everything is all in one place in a consistent location.

    Thread Starter nootkan

    (@nootkan)

    Okay…so you’re saying that I should be placing the

    function add_theme_scripts() {
      wp_enqueue_style( 'style', get_stylesheet_uri() );
     
      wp_enqueue_style( 'slider', get_template_directory_uri() . '/css/slider.css', array(), '1.1', 'all');
     
      wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);
     
        if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
          wp_enqueue_script( 'comment-reply' );
        }
    }
    add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]

    inside my setup function as described inside the handbook and not the individual enqueue calls?

    • This reply was modified 7 years, 12 months ago by nootkan.
    • This reply was modified 7 years, 12 months ago by bdbrown.
    • This reply was modified 7 years, 12 months ago by bdbrown.
    Moderator bcworkz

    (@bcworkz)

    No, not in the setup function, but not individual calls directly from functions.php either. Calls enqueuing styles and scripts for the front end should always be within a callback added to “wp_enqueue_scripts” action.

    A typical functions.php file:

    <?php
    /* Header comment information not required but useful */
    
    if ( ! function_exists( 'my_theme_setup' ) ) :
    function my_theme_setup() {
      // setup functions here, but nothing enqueued -- wrong action for that
    }
    endif;
    add_action( 'after_setup_theme', 'my_theme_setup' );
    
    /* WRONG! Not here!!
    wp_enqueue_script( 'respond.min', get_template_directory_uri() . /js/respond.min.js', array ( 'jquery' ), 1.0, true);
    */
    
    // Correct!!
    function add_theme_scripts() {
      // Add any other front end enqueue calls here
      wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);
    }
    add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );
    Thread Starter nootkan

    (@nootkan)

    Not sure what happened with my previous post as I did highlight and select code button. Sorry about that. Thanks for the reply. I think I understand it now and will continue on through the manual when I can find some more time.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Theme Handbook Confusion’ is closed to new replies.