WiscWeb WordPress - Creating and Invoking Custom WordPress Hooks

WordPress hooks provide a way for executing a function at a specific point in plugins and themes.


Action Hooks

 

Action hooks provide a way for executing a function at a specific point in plugins and themes. Functions are attached to action hooks using the add_action() function and then invoked by a do_action() function in a plugin or template. The do_action() function invokes all functions attached to the hook. 

 

Action Hook Code Example:

 

In the functions.php file:

The following functions will be attached to the hook.

 

function uw_example_action() {

echo "First function ";

}

add_action('uw_example_action_hook', 'uw_example_action', 0);

 

function uw_example_action_part_2() {

echo "Second function";

}

add_action('uw_example_action_hook', 'uw_example_action_part_2', 1);

 

 

 

In the template or plugin file:

The following code will invoke all functions attached to the hook in the functions.php file.

 

<?php do_action('uw_example_action_hook'); ?>

 

The output of this hook would be: 'First function Second function'.

 

 

Filter Hooks

 

Filter hooks provide a way for functions to modify data of other functions. Functions are attached to filter hooks using the add_filter() function and then invoked by passing content to an apply_filter() function by name.

 

Filter Hook Code Example:

 

In the functions.php file:

The following function will be attached to the hook.

 

function uw_example_filter( $param ) {

return "This filter prepends " . $param;

}

add_filter( 'uw_example_filter_hook', 'uw_example_filter', 1);

 

function uw_example_filter_part_2( $param ) {

return "text to this value: " . $param;

}

add_filter( 'uw_example_filter_hook', 'uw_example_filter_part_2' , 0);

 

 

 

In the template or plugin file:

The following code will invoke the filters attached to the hook in the functions.php file.

 

 

<?php echo apply_filters( 'uw_example_filter_hook', 'Example text to modify' ); ?>

 

The output of this hook would be: 'This filter prepends text to this value: Example text to modify'.

 

 

Setting Hook Priority

 

Hooks will execute in the order of the priority you set. If the priority is not user-defined it will default to 10. The following is an example of the order of execution based on the user-defined priorities.

 

Execute First: add_action('uw_example_action_hook', 'function_name', 0);

Execute Second: add_action('uw_example_action_hook', 'function_name', 1);

Execute Third: add_action('uw_example_action_hook', 'function_name', 2);

 

 

 

Naming Conflicts

 

To avoid naming conflicts between your custom hook, theme hooks and plugin hooks be sure to add a unique prefix your hook's name: e.g. 'uw_'.

 

 

 

 

UW Child Theme Hook Usage Example

 

To add Google Tag Manager to a UW Child Theme you’re able to use the following hooks.  Simply add the following to your functions.php file after you have replaced ‘GTM-EXAMPLE’ in each script tag with your Google Tag Manager Container ID. The Google Tag Manager Container ID can be found within the Accounts Tab on Google Tag Manager.

 

// add tag manager to head

function tag_manager_head() {

    if ( !is_user_logged_in() ) { ?>

    <!-- Google Tag Manager -->

    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':

    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],

    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=

    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);

    })(window,document,'script','dataLayer','EXAMPLE-ID');</script>

    <!-- End Google Tag Manager -->

    <?php }

}

add_action('wp_head', 'tag_manager_head', 0);

 

// add tag manager to body

function tag_manager_body() {

    if ( !is_user_logged_in() ) { ?>

<!-- Google Tag Manager (noscript) -->

<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=EXAMPLE-ID"

height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>

<!-- End Google Tag Manager (noscript) -->

    <?php }

}

add_action('uw_after_body_open_tag', 'tag_manager_body', 0);

 

 

 

Default WordPress Hook References

 

Plugin API / Filter Reference

https://codex.wordpress.org/Plugin_API/Filter_Reference

 

Plugin API / Action Reference

https://codex.wordpress.org/Plugin_API/Action_Reference 

 

 

 

Custom WordPress Hook References

 

WordPress Custom Hook Documentation

https://developer.wordpress.org/plugins/hooks/custom-hooks/

 

Action Hook Documentation

https://developer.wordpress.org/plugins/hooks/actions/

 

Filter Hook Documentation

https://developer.wordpress.org/plugins/hooks/filters/

 

Function: do_action()

https://developer.wordpress.org/reference/functions/do_action/

 

Function: add_action()

https://developer.wordpress.org/reference/functions/add_action/

 

Function: apply_filters()

https://developer.wordpress.org/reference/functions/apply_filters/

 

Function: add_filter()

https://developer.wordpress.org/reference/functions/add_filter/

 

 




Keywords:WordPress, hooks, action, filter, function, child theme, UW theme, WordPress hook   Doc ID:73134
Owner:Jonathan D.Group:WiscWeb CMS
Created:2017-05-05 13:29 CDTUpdated:2017-05-10 19:23 CDT
Sites:WiscWeb CMS
CleanURL:https://kb.wisc.edu/creating-and-invoking-custom-wordpress-hooks
Feedback:  0   0