You are here:Home » WordPress » Create a Custom Action Hook Wordpress

Create a Custom Action Hook Wordpress

The Codex says:
Hooks are provided by WordPress to allow your plugin to 'hook into' the rest of WordPress; that is, to call functions in your plugin at specific times, and thereby set your plugin in motion. There are two kinds of hooks:
  1. Actions: Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur. Your plugin can specify that one or more of its PHP functions are executed at these points, using the Action API.
  2. Filters: Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen.
Action hooks are triggered by events in WordPress and are providedby scores – and every time a hook is triggered all functions (callbacks) you or anybody else attached to it are executed automagically.
There’s no magic really. All available hooks are stored in a global variable called $wp_filter, a PHP associative array that has hook names for keys.
You hook your functions to an action using add_action()  - add_action() adds your function name to the $wp_filter element with the key that is equal to the hook name you specified. If such an element does not exist it is created – added to $wp_filter.
Your functions attached to a hook are executed when you call do_action(‘action_name_here’). do_action() checks $wp_filter for ‘action_name_here’ and executes all functions attached to it, if any.
This allows you to create your own custom action hooks in two and a half easy steps, as seen below (somewhere in my theme’s functions.php):
<?php

/* a custom action hook */

/*
 * 1. Create your own custom action hook named 'the_action_hook'
 *    with just a line of code. Yes, it's that simple.
 *    
 *    The first argument to add_action() is your action hook name
 *    and the second argument is the name of the function that actually gets
 *    executed (that's 'callback function' in geek).
 *
 *    In this case you create an action hook named 'the_action_hook'
 *    and the callback function is 'the_action_callback'.
 */

add_action('the_action_hook', 'the_action_callback');

/*
 * 2. Declare the callback function. It prints a sentence.
 *    Note that there is no return value.
 */

function the_action_callback()
{
echo '<p>WordPress is nice!</p>';
}

/*
 * 3. When you call do_action() with your action hook name
 *    as the argument, all functions hooked to it with add_action()
 *    (see step 1. above) get are executed - in this case there is
 *    only one, the_action_callback(), but you can attach as many functions
 *    to your hook as you like.
 *
 *    In this step we wrap our do_action() in yet another
 *    function, the_action(). You can actually skip this step and just
 *    call do_action() from your code.
 */

function the_action()
{
do_action('the_action_hook');
}

?>
and execute them (my theme’s index.php):
<!DOCTYPE html>

<html>
<head>
<title><?php bloginfo('name'); ?></title>
<link rel="stylesheet" type="text/css" media="all"
href="&lt;?php bloginfo( 'stylesheet_url' ); ?>" />
<?php wp_head(); ?>
</head>
<body>
<div>
<div>
<h1><a href="&lt;?php echo esc_url( home_url( '/' ) ); ?>">
<?php blogInfo( 'name' ); ?></a></h1>
</div>
<div>
<?php if(have_posts()): while(have_posts()): the_post();?>
<h2><a href="&lt;?php the_permalink() ?>"><?php the_title() ?></a></h2>
<div>
<?php the_content(); ?>
<!-- Yay! this line should print 'WordPress is nice!'
                             after all your posts. -->
<?php the_action(); ?>
<!-- This line should also print 'WordPress is nice!'.
                             This time no wrapper function for do_action(). -->
<?php do_action('the_action_hook'); ?>
</div>
<?php endwhile; endif; ?>
<?php wp_footer(); ?>
</div>
<div>
<?php get_sidebar(); ?>
</div>
</div>
</body>
</html>

0 comments:

Post a Comment