Minimal WordPress CSS plugin example

Also see:
Minimal WordPress JS plugin example
Minimal WordPress Shortcode plugin example

This plugin adds a static CSS file to WordPress.

First, create a directory for your plugin in wp-content/plugins/, e.g. wp-content/plugins/my-css-plugin

Save the following code as functions.php in the plugin folder, e.g. wp-content/plugins/my-css-plugin/functions.php

<?php
/*
Plugin Name: My CSS plugin
*/

function my_plugin_enqueue_css(){
    wp_enqueue_style('my-plugin-stylesheet', plugins_url('/style.css', __FILE__), false, '1.0.0', 'all');
}
add_action('wp_enqueue_scripts', "my_plugin_enqueue_css");

Next, save your desired CSS file in style.css in the plugin folder, e.g. wp-content/plugins/my-css-plugin/style.css. Example for a stylesheet:

/* This is just an example CSS and does not have any specific meaning! */
.my-plugin-class {
    font-weight: bold;
}

Now activate your plugin your WordPress admin area.

Your CSS will be loaded for each WordPress page until you deactivate the plugin.

Note that if you are using a CSS-optimizing plugin like Autoptimize, you might not actually see your CSS file as separately loaded stylesheet as it is compiled into the single Autoptimize CSS. You style will still be loaded!