Lets say we have a do_action like this:

do_action('some_action');

Doesn't this mean that there has to be a some_action() function somewhere in the wordpress like this for example:

function some_action(){
   /* blah blah blah*/
}

My question comes from a do_action() at the storefront theme of woocommerce that has a line in header.php like this:

do_action( 'storefront_header' );

But I haven't been able to find any storefront_header() function so I started wondering perhaps not all do_actions are coming with a function of their own. Is it possible?

up vote 7 down vote accepted

The param used in do_action isn't a function is just a tag that represents the action called, the proper way to use an action is attaching functions with add_action.

You can learn more from https://developer.wordpress.org/reference/functions/do_action/

No.

The some_action in do_action( 'some_action' ); is an action hook, and allows you to cause actions to happen at a specific point in time during WordPress's lifecycle. If an action hook happens to share a name with a function, it's a coincidence (or often a convenience).

From the glossary:

… an Action is a PHP function that is executed at specific points throughout the WordPress Core.

Developers can create a custom Action using the Action API to add or remove code from an existing Action by specifying any existing Hook. This process is called "hooking".

Glossary: Action

Action hooks (and their cousins, filter hooks) are part of WordPress's rich Plugin API.

You might want to have a look in storefront/inc/storefront-template-hooks.php see https://github.com/woocommerce/storefront/blob/1790927f8cab022bc96847fb0efc4f2f2ff6b2aa/inc/storefront-template-hooks.php

You can see the action in here, and the relevant functions (and order):

add_action( 'storefront_header', 'storefront_skip_links',                       0 );
add_action( 'storefront_header', 'storefront_site_branding',                    20 );
add_action( 'storefront_header', 'storefront_secondary_navigation',             30 );
add_action( 'storefront_header', 'storefront_primary_navigation_wrapper',       42 );
add_action( 'storefront_header', 'storefront_primary_navigation',               50 );
add_action( 'storefront_header', 'storefront_primary_navigation_wrapper_close', 68 );

With this first one, selected for example, the function here is storefront_skip_links, which is in storefront/inc/storefront-template-functions.php (line 279 at time of writing!) and looks like this:

if ( ! function_exists( 'storefront_skip_links' ) ) {
    /**
     * Skip links
     *
     * @since  1.4.1
     * @return void
     */
    function storefront_skip_links() {
        ?>
        <a class="skip-link screen-reader-text" href="#site-navigation"><?php esc_attr_e( 'Skip to navigation', 'storefront' ); ?></a>
        <a class="skip-link screen-reader-text" href="#content"><?php esc_attr_e( 'Skip to content', 'storefront' ); ?></a>
        <?php
    }
}

Hope this helps you to understand how this works in practice!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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