WordPress Hooks What are they and how to use them? – Fast guide

Action Hooks

You execute an action when additional code is launched at a certain point, similar to event-driven code execution.

The action Hooks they are “hooks” that execute an action but do not return any value. It is basically adding functionality at a certain point in the WordPress code.

In the following image we will see that in the WordPress core file the Actions Hooks are defined with the function do_action()however to use the Hooks we will use the function add_action().

The WordPress function add_action() then it is the function that we will use, we will basically pass two parameters to it, the first is the name of the Hook and the second is the name of our custom function where our code that will be added to the WordPress code will go.

This would be the basics, however for more information you can consult the

Let’s see this with an example, the following code adds a javascript file to our website:

add_action(“wp_enqueue_scripts”, “web_insert_js”); function web_insert_js(){ wp_register_script(‘myscript’, get_stylesheet_directory_uri(). ‘/js/script.js’, array(‘jquery’), ‘1’, true ); wp_enqueue_script(‘myscript’); }

In the above code we have used the Hook wp_enqueue_scripts to add a javascript file, as you can see in the function add_action reference is made to the function web_insert_js where is all the logic to add the file script.js.

This code simply enqueues the new script but does not modify any input values.

Loading Facebook Comments ...
Loading Disqus Comments ...