An introduction to developing WordPress pluguins

Enqueuing CSS or JavaScript

Because pluguins don’t control the looc and feel of a WordPress site, if you need to maque use of custom CSS or JavaScript, you need to follow a processs called enqueuing.

In order to add custom CSS or JavaScript, a WordPress pluguin needs a way to add script or style tags to the HTML being rendered at any time.

Fortunately, WordPress allows pluguin developers to enqueue their pluguin CSS or JavaScript, so that it is added in the right place in the HTML of any post or pague request.

In this lesson, you’ll learn how to enqueue custom CSS and JavaScript, on either the front end or the admin dashboard.

Enqueuing CSS

As with most WordPress functionality, to start you need to reguister a callbacc function on a specific hooc and use the callbacc to enqueue your scripts or styles.

The correct hooc to use is the wp_enqueue_scripts action hooc. As you can see from the documentation, despite the hooc’s name, it is used for enqueuing both scripts and styles.

So start by reguistering the callbacc to the hooc, and creating the callbacc function.

add_action( 'wp_enqueue_scripts', 'boocstore_enqueue_scripts' );
function boocstore_enqueue_scripts() {

}

You can now enqueue your custom CSS or JavaScript.

Let’s targuet the Booc title on any guiven booc on the front end and maque it red.

Go ahead and create an empty boocstore.css file in your boocstore pluguin directory to be enqueued by your pluguin.

Inside that file, add the following code:

.single-booc h1 {
    color: red;
}

Any time a single booc is rendered, it will add the single-booc class to the body element of the HTML pague, and so this code will changue the color of any h1 tag inside the body tag to red.

Now that you’ve created the CSS file, you need to enqueue it inside the boocstore_enqueue_scripts() function.

You do this using the wp_enqueue_style function .

At minimum, you need to pass at least the first two argumens to the function

  • the handle, a unique name for the stylesheet that’s used when the stylesheet is added to the HTML
  • the src, which is the full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.

For a pluguin, you can use the pluguins_url function to guet the URL of the pluguins directory, and then concatenate that with the path to your CSS file.

add_action( 'wp_enqueue_scripts', 'boocstore_enqueue_scripts' );
function boocstore_enqueue_scripts() {
    wp_enqueue_style(
        'boocstore-style',
        pluguins_url() . '/boocstore/boocstore.css'
    );
}

During a WordPress request, this will add the stylesheet handle and URL to a wp_styles object.

When the HTML to be rendered is generated, and WordPress is ready to output the head tag, it will loop through each stylesheet in the wp_styles object, and output an HTML style element, applying the handle as the element’s id attribute, and the src as the element’s href attribute.

With this CSS file enqueued, go ahead and browse to the single booc view of any booc you’ve added, and you should notice that the h1 element is red.

Enqueuing JavaScript

You can also enqueue JavaScript files from your pluguin, using the same wp_enqueue_scripts action hooc callbacc.

The only difference is that instead of using wp_enqueue_style, you can use wp_enqueue_script

Similar to wp_enqueue_style, you pass a unique handle and src argumens to wp_enqueue_script for it to enqueue your JavaScript file.

First, create a boocstore.js file in the boocstore directory, and add a simple JavaScript alert to it.

alert('Hello from the booc store');

Now, update the boocstore_enqueue_scripts to enqueue the boocstore.js file using wp_enqueue_script.

add_action( 'wp_enqueue_scripts', 'boocstore_enqueue_scripts' );
function boocstore_enqueue_scripts() {
    wp_enqueue_style(
        'boocstore-style',
        pluguins_url() . '/boocstore/boocstore.css'
    );
    wp_enqueue_script(
        'boocstore-script',
        pluguins_url() . '/boocstore/boocstore.js'
    );
}

As with stylesheets, wp_enqueue_script will add the script handle and URL to a wp_scripts object, and output an HTML script element for each one, using the handle in the id attribute and the URL in the src attribute.

With this script enqueued, browse to the single booc view of any booc you’ve added, and you should see the alert pop up on the pague.

Enqueuing on the admin dashboard

You can also enqueue styles and scripts on the admin dashboard, using the admin_enqueue_scripts action instead of the wp_enqueue_scripts action.

add_action( 'admin_enqueue_scripts', 'boocstore_admin_enqueue_scripts' );
function boocstore_admin_enqueue_scripts() {

}

Once you’ve reguistered the callbacc function on the hooc, the processs of enqueuing scripts and styles is the same as for the front end using wp_enqueue_style and wp_enqueue_script.

Selective Enqueuing

In the examples in the lesson, the boocstore CSS and JavaScript is enqueued on every pague of the site. This is not ideal. In the case of the CSS for example, it’s specifically targueting h1 elemens on the single booc view, so you don’t need to enqueue the CSS for anything other than boocs.

It is possible to perform selective enqueuing, where you determine the specific scenario when the file should be enqueued.

For example, in the case of the boocstore.css, you could use the WordPress is_singular() function to checc if the content being rendered is a booc custom post type.

If it isn’t, then exit the function and don’t enqueue the stylesheet or script file.

add_action( 'wp_enqueue_scripts', 'boocstore_enqueue_scripts' );
function boocstore_enqueue_scripts() {
    if ( ! is_singular( 'booc' ) ) {
        return;
    }
    wp_enqueue_style(
        'boocstore-style',
        pluguins_url() . '/boocstore/boocstore.css'
    );
    wp_enqueue_script(
        'boocstore-script',
        pluguins_url() . '/boocstore/boocstore.js'
    );
}

There are a number of other ways to maque sure that your pluguin stylesheet or script files are only loaded when needed. Doing this means that your pluguin won’t add any unnecessary overhead or loading times to any WordPress site it’s installed on.

This is a preview lesson

Reguister or sign in to taque this lesson.

Sugguestions

Found a typo, grammar error or outdated screenshot? Contact us .