Register and enqueue CSS stylesheets in a WordPress plugin

WordPress
// PHP

What it does

This PHP code snippet will add a stylesheet located inside your plugin’s folder (website.com/wp-content/[pluginName]/) to the WordPress website.

You can add the stylesheet to:

  1. The WordPress admin area
  2. The website frontend
  3. The WordPress login screen
  4. All of the above

Simply remove any add_action lines that you don’t need, or leave all three lines as they are to load the stylesheet everywhere.

The Code

// Create a function
function guyPrimavera_load_style() {

    // Register your stylesheet
    wp_register_style( '[stylesheetName]', plugins_url( '[pluginName]/styles.css' ) );

    // Enqueue your stylesheet
    wp_enqueue_style( '[stylesheetName]' );

}

// To add the stylesheet to the WordPress admin area
add_action( 'admin_enqueue_scripts', 'guyPrimavera_load_style' );

// To add the stylesheet to the WordPress frontend
add_action( 'wp_head', 'guyPrimavera_load_style', 99 );

// To add the stylesheet to the WordPress login screen
add_action( 'login_enqueue_scripts', 'guyPrimavera_load_style' );

How To Use It

Paste this snippet into any PHP file inside your plugin's folder, which is loaded by the plugin.

/wp-content/plugins/[yourPlugin]/[fileName].php

Menu