Beginner Tutorial for WordPress Actions add_action() | Coding Like a Pro
Beginner Tutorial for WordPress Actions add_action()

Beginner Tutorial for WordPress Actions add_action()

Introduction

WordPress has been around for a while as a choice site-building platform for many developers across the globe. It has certainly dominated the programming market for quite a while. This doesn’t come as a surprise.

The platform is constantly innovating features that stand it out and address the challenges modern websites face. One of such features is what we know as WordPress Actions.

This tutorial will deal in details, the meaning of this concept. It’ll equally include very illustrative examples to better aid your understanding of WordPress actions.

But then, note that there are elements that are related to this feature. They include Filters and Hooks. They form the family of WordPress Actions with their unique functions.

WordPress Actions, what are they?

We presume that you understand the basics of WordPress. Good, that’s a great way to start.

Now, we’ll move on to the core part of this topic – WordPress actions. So, what are they? In simple terms, these are hooks made ready for developers to modify at some point during code blocks to change the default functionalities of WordPress.

A perfect instance of this is when developers include some plug-in codes when executing particular WordPress actions.

More so, it is worthy of note that these actions are many in WordPress. You can research further on them on the internet.

More Examples

The best way to put you in the proper perspective of what WordPress action is all about is to include more specific examples of its function. Don’t be scared. It isn’t one hell of a difficult task. All that’s required is your rapt attention.

In our first example, you’ll see how to easily and simply add Javascript code to WordPress theme.

function codinglikeproScript() {
wp_enqueue_script('nav-script', get_template_directory_uri() . '/js/nav-script.js', array(), '', true);
}
add_action('wp_enqueue_scripts', 'codinglikeproScript');

WP Actions Explained Further

We’ll dig deeper into what this element is.

Instruction above clearly stated the name of the file is codinglikeproScript().

Also, from the example, it is clear how we assigned names to the functions. While we used wp_enqueue_script() to indicate internal function of WordPress, wp_enqueue_script() was used for the arguments.

Let’s dissect the instruction a little further below:

$handle – is the script’s name

src – internal assisting function of WordPress named get_template_directory_uri() enables this instruction.

With this function, we can locate the URL of the theme directory. It equally grants us access to the path of the sheet file through the theme source directory.

$deps – this element represents script dependencies – it collects many different dependencies available prior to the time you load the script.

$ver – this is a version. We’ll just quote WordPress’s Codex:

$ver – (string|bool|null) (Optional) this element identifies the version number of the script. We attach it to the domain as a query string for cache purpose.

When you set version as false, then a version that equals the installed WordPress version is immediately added. But, if it’s set as null, we add no version.

So, the original element is false

$in_footer – The end argument. The element determines how you will load the script in the footer or header.

A lot many different developers fall within this category. You can check the scripts such as Google tags, analytics, etc. Also check the library files such as owl carousel.js, lightgallery.js, etc.

After the check, you proceed to import onto your WordPress site. But, understand that you must have to clearly set the argument to enable the script to be properly imported.

While false implements the header, true implements footer

Wrap up

add_action():

We have reached the last lap. Sure, you must have been doing very well by following the tutorial with rapt attention. Well, it is time we wrap things up and allow you the time to practice more to learn better.

You must have equally been able to grasp the lesson. But, if you are still confused about a thing or two about WP actions, don’t bother much.

We will do well to explain in clearer terms so you can follow.

add_action() will do the last job. It collects the function we have written and runs it just while the wp_enqueue_script() is running.

Meanwhile the custom element is codinglikeproScript(). The add_action() function hooks it into.

Just while the core hook wp_enqueue_script() of WordPress is running, it collects the custom function and runs along with it. This purely an easy script.

Moreover, you need to equally note that WP actions are just one part of the family. Both actions and filter in the WordPress family complement each other. While we may wish to deliberate on Filters in future articles, understand that you need to learn further on this topic. There’s just so much you can do with WordPress actions.

Spread the love
2 votes, average: 4.50 out of 52 votes, average: 4.50 out of 52 votes, average: 4.50 out of 52 votes, average: 4.50 out of 52 votes, average: 4.50 out of 5 (2 votes, average: 4.50 out of 5)
You need to be a registered member to rate this.
Loading...

Leave a Reply