0

dear I unable to understand of simple function call & do_action function call.

do_action:

I hook function and I place this in my one template file for use in header template.

add_action('theme_document', 'run_this')
function run_this()
{
<!DOCTYPE html>
<html class="no-js">
<head>
}

then I call it on my header do_action('theme_document') and it worked. But when it also worked in simple function call, then why need of call action hook?

This code also work when I use this by a simple function call like:

function run_this()
{
<!DOCTYPE html>
<html class="no-js">
<head>
}

and I call this function in my header and it also worked

run_this();
3

The main benefit with hooks ( such as add_action ) is that they allow you to add additional logic very easily based on priority. The only way to achieve the same thing with a standalone function is to edit the function directly. You wouldn't want to do this in premium theme ( or core ) as any time there's an update your function will be overwritten.

Take the following example which outputs a title into the <title> tag. First we register our hook and put the call into the header.php file in it's respective spot.

/**
 * Custom Theme Title
 */
function theme_custom_title() {
    do_action( 'theme_custom_title' );
}

header.php HTML

<title><?php echo theme_custom_title(); ?></title>

Now that our custom hook is set up let's discuss priorities, the 3rd parameter of add_action. Priorities allow us to run additional functions in order of priority in Ascending order, let's register 3 hooks to our Custom Hook.

function custom_title_one() {
    echo "Hook One ";
}
add_action( 'theme_custom_title', 'custom_title_one', 10 );

function custom_title_two() {
    echo "Hook Two ";
}
add_action( 'theme_custom_title', 'custom_title_two', 20 );

function custom_title_three() {
    echo "Hook Three ";
}
add_action( 'theme_custom_title', 'custom_title_three', 1 );

If we run the above we'll see the following output in the <title> tag:

Hook Three Hook One Hook Two

While it may seem trivial, this is fantastic for functions / hooks you do not want to actually overwrite. We could add these three hooks into a Child Theme and it will always run these hooks no matter how many times the Parent Theme updates. If the function takes parameters you could conditionally handle and manipulate the parameter and return different output. This is much more difficult to achieve as a stand alone function and the purpose is to allow developers to modify the main function without actually overwriting its core functionality.

1

Actions and filters are intended to allow third part code (though Core uses a lot of actions and filters as well) to modify how WordPress works without hacking the code. That is, hooks allow WordPress to be extended and altered without requiring hackers to modify the main body of code, which would be a nightmare when a new release comes out.

Hooks are not meant simply as ways to fire a function. If that is all you are doing, you are using them incorrectly. You should be using hooks and filters to run code that modifies other code. For example, the incredibly useful pre_get_posts modifies the way that WP_Query operates:

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-1,-1347' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

To do that without using the hook would mean rewriting WP_Query or rolling your own SQL.

Reference: Clarification on filters and hooks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.