Theme Handbook Confusion
-
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.
- The topic ‘Theme Handbook Confusion’ is closed to new replies.