WP Actions aka WordPress Actions

WP Actions simplified

If you’re a web developer who works or plans to work in WordPress dev environments you’ve probably heard the term WordPress Actions, the soo called wp actions.
In this article, we are going to cover the meaning, roles, and examples of  WordPress Actions which is a part of the family of Actions, Hooks, and Filters.

WordPress Actions, what are they?

Well, I will go ahead and assume you know a bit of WordPress and go on ahead. In short wp actions are hooks that the WordPress core system launches at predefined and specific points during an execution of other functions and code blocks or when specific events occur. For example, plugin developers tie some plugin code to wp actions.
Just so you know if you don’t already, WordPress has a lot of defined action, you can browse those actions here: WordPress Actions
Let’s cover some examples and I’m sure you will get the hang of it, trust me it’s not so scary when you really dive and try to understand the process that occurs.
The first example is a simple implementation of the way we include a JAVASCRIPT file to our WordPress Theme. It goes like this:

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

wp action breakdown:

I will go ahead and break down this patch of code for you:
As you can see here the functions name is ishayScript().
The second thing we do is to call WordPress’s inner function called wp_enqueue_script() The arguments wp_enqueue_script() accepts are as follows:
$handle– script name,
src –  manifests with the use of WordPress’s inner helper function called get_template_directory_uri() . This provides us with the themes directory URI, its followed by a relative path by us from the themes parent directory to the script file location.
$deps – script dependencies – The next is an array of dependencies before loading your script.
$ver – The following argument is a version, i will simply quote the WordPress Codex:

$ver
(string|bool|null) (Optional) String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
Default value: false

$in_footer – The last argument is very important, Its called $in_footer and It decides whatever to load the script in the header or the footer.
Many good people fall here, check the scripts(google tags, analytics etc) and library files( owl carousel.js, lightgallery.js etc) you import onto your WordPress site. Meaning, if the scripts require to be imported into the head/footer section so be sure to set the argument accordingly.
false means onto the head truemeans onto the footer.

The last part

add_action().
Now, this is the money time if you were reading closely and got what I said earlier you should be able to tell me what happens next, if not don’t sweat it.
I will break this one down for you 🙂 What happens is that the add_action() function takes our custom written function and runs it when the hook wp_enqueue_script() runs.
Our custom function: ishayScript() is so-called ‘hooked’ into action by our add_action() statement. Soo, when the WordPress core hook wp_enqueue_script() runs it hooks our custom function too, and in this case, it’s just a simple script. Another important thing to remember about the family of  WP actions is only half the part of the amazing useability of HOOKS, ACTIONS, and FILTERS. If WordPress actions are the bread WordPress filters are the butter.
We will talk about WordPress Filters in a different article, so stay tuned 🙂
Think of the great things you can build with it, it’s an amazing power.
I hope things are a bit clear with wp actions, feel free to browse the references below for more info and documentation,
WordPress Actionswp_enqueue_script(), add_action().

wordpress actions

Leave a Reply

Your email address will not be published. Required fields are marked *