Trying to properly register and enqueue stylesheet in WP but nothing is rendered

I’m told it’s very important to register and enqueue styles and scripts in WordPress even though it’s a real pain in the behind. I had no problem with the scripts however when I try the styles, WordPress shows nothing in the code for stylesheets. I’d like to get in the habit of building my themes the correct way so I’d like to learn how this is done, but I can’t understand why my css is not getting added to the code when the scripts are added. See code to functions file below:

<?php

function alpha_scripts(){

  $script = get_template_directory_uri() . '/assets/scripts/';

  wp_register_script('bootstrap-js', $script . 'bootstrap.min.js', array('jquery'),'', false);

  //wp_enqueue_script('jquery');
  wp_enqueue_script('bootstrap-js');
}

function alpha_styles(){

  $style = get_template_directory_uri() . '/assets/styles/';

    wp_register_style('bootstrap-css', $style . 'bootstrap.min.css','','', 'screen');
    wp_register_style('alpha-css', $style . 'alpha.css','','', 'screen');

    wp_enqueue_style('bootstrap-css');
    wp_enqueue_style('alpha-css');
}

function alpha_menus() {
  register_nav_menus(
    array(
      'main-menu' => __( 'Primary Menu' ),
      'footer-menu' => __( 'Footer Menu' )
    )
  );
}

add_action('wp_enqueue_scripts', 'alpha_scripts');
add_action('wp_enqueue_styles', 'alpha_styles');
add_action('after_setup_theme', 'alpha_menus');

?>

What am I doing wrong here? I’ve tried using different references for the url, like get_template_directory_uri() and get_stylesheet_directory_uri() but as I suspected those two made no difference.

Related posts

1 comment

  1. It looks like your registering and enqueueing correctly; but I suspect that the function itself is not being called because the action you are attaching it to isn’t one defined by WordPress.

    add_action('wp_enqueue_styles', 'alpha_styles');

    should be:

    add_action('wp_enqueue_scripts', 'alpha_styles');

    wp_enqueue_scripts is the hook that should be used for both scripts and styles. (See: https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts)

    If your unsure about a hook in the future, you can check if it exists in the action reference here: https://codex.wordpress.org/Plugin_API/Action_Reference

Comments are closed.