Yogesh Chauhan's Blog

How to include CSS and JavaScript files in your WordPress plugin?

in WordPress on June 10, 2021

When you’re developing a WordPress plugin or theme, you would probably want to make it look pretty using CSS and add some JavaScript or maybe you just want to import some fonts using a third party URL.

For all those things mentioned above we need to determine both, the plugin and content directories.

WordPress has tons of hooks and functions readily available for almost everything, almost! We can make use of those functions to achieve our goal of importing the CSS and JS files.

Use the WordPress functions to find a directory path rather than hard coding an absolute path to the wp-content directory as WordPress allows you to put your themes, plugins, uploads folders anywhere you’d like.

__FILE__ constant

PHP has a great constant, __FILE__, that resolves symlinks automatically.

So, if your plugins folder is symlinked then your hard coded JavaScript or CSS files path won’t work properly.

How to load files?

You can just

You can use plugins_url() function to include CSS, JavaScript or other external files. Just like this:

plugins_url( 'script.js', _FILE_ );

The code above will return the following path:


How to load plugin scripts to frontend page?

You can use the same functions that you use to load your theme CSS and JavaScript files to load the plugin script files into the frontend page.

To load CSS files you can use wp_enqueue_style() and to load JavaScript files you can use wp_enqueue_script() function. In both cases, you’ll pass the file URL using plugins_url().

Just like this:

function yc_load_scripts($hook) {     
  wp_enqueue_script( 'script-js', plugins_url( 'js/script.js', __FILE__ ), array(), '1.0.0' );
  wp_register_style( 'style',    plugins_url( 'style.css',    __FILE__ ), false,   '1.0.0' );
add_action('wp_enqueue_scripts', 'yc_load_scripts');

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
JavaScript Input Validation Theme Park Example using throw StatementJavaScriptHow to switch dark and light themes using pure CSS?CSSHow to add a Bar Chart in Angular App?AngularLearn how to give a temporary name to a column or to a table using SQL AliasesSQL/MySQLCommon Table Expressions (CTE) in PostgreSQLPostgresaddcslashes() and addslashes() String Functions in PHPPHP