Home > Back-end >  How can I create a WordPress Plugin with custom CSS?
How can I create a WordPress Plugin with custom CSS?

Time:11-04

I want to create a WordPress Plugin where I want to add some css, and upon activation, those css will be in effect and be viewing in the website. Upon deactivation, the css will also be out of effect.

Any how I can do that? Or any documentation I can take help from?

CodePudding user response:

You can follow Plugin Handbook for creating a plugin https://developer.wordpress.org/plugins/.

create a CSS file in plugin directory then upon plugin activation hook you can enqueue the CSS file using action 'wp_enqueue_style'.

CodePudding user response:

You can add this to your main file to load your css file:

function add_my_css() {
    $plugin_url = plugin_dir_url( __FILE__ );
    wp_enqueue_style( 'style',  $plugin_url . "/css/plugin-style.css");
}

add_action( 'wp_enqueue_scripts', 'add_my_css' );

Then create your plugin-style.css file and write your css.

Hope this helps

  • Related