add_action

add_action is a function which you’ll see in WordPress, Genesis & other theme files.

You’ve probably seen code in a file which looks something like this:

add_action( 'genesis_before_header', 'hook_test' );

To understand how add_action() works with different hooks in WordPress & Genesis, you’ll need to learn the basics of how to use it in code.

In the code above:

add_action is the WordPress function.
genesis_before_header is the hook name.
hook_test is the name of your function call.

Video Walk Thru

This video walks you thru this post and shows you the result of testing the example code snippets.

Available on request for registered users.

There are different types of action hooks, however the most commonly used are theme specific hooks like genesis_before_header and WordPress specific hooks like loop_start.

Theme Specific Hooks

Using a theme specific hook enables you to print text like “Hello World”

Add the following custom function to your child themes functions file.

The code will echo the text “Hello World” in the genesis_before_header hook location.

add_action( 'genesis_before_header', 'hook_test1' );
function hook_test1() {
    echo "Hello World";
}

Now change the hook in the above code to genesis_after_header to see the different location the text “Hello World” is printed.

add_action( 'genesis_after_header', 'hook_test2' );
function hook_test2() {
    echo "Hello World";
}

Here’s a visual presentation of all Genesis action hooks.

WordPress Hooks

WordPress includes over 1700 hooks.

1 of the action hooks included in WordPress is named loop_start. You can use loop_start in any WordPress theme.

Add the following PHP code to your child themes functions file to test the loop_start hook in a custom function:

add_action( 'loop_start', 'hook_test3' );
function hook_test3() {
    echo "Hello World";
}

The only difference between the above code and the code we tested previous to that is the change of hook from genesis_after_header to loop_start.

This shows you the most basic usage of a WordPress action hook used with the add_action function.

The add_action function enables you to add content or execute a function in a specific position in your theme or at a specific execution point or time.

Related

Was This Tutorial Helpful?

Free

$0

Access only to all free tutorials per month.



Monthly

$75

Access to 10 premium tutorials per month.


Tutorial Request


Includes code guarantee and coding support.

Yearly

$500

Access to 15 premium tutorials per month.


Monthly Tutorial Request


Includes code guarantee and priority coding support.