<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Digging into WordPress &#187; actions</title>
	<atom:link href="http://digwp.com/tag/actions/feed/" rel="self" type="application/rss+xml" />
	<link>http://digwp.com</link>
	<description>Take your WordPress skills to the next level.</description>
	<lastBuildDate>Thu, 09 Feb 2012 19:03:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Precision Targeting with Custom Action Hooks</title>
		<link>http://digwp.com/2009/09/wordpress-action-hooks/</link>
		<comments>http://digwp.com/2009/09/wordpress-action-hooks/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 05:00:11 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[actions]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[hooks]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[Theme]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=744</guid>
		<description><![CDATA[WordPress&#8217; powerful action-hook system makes it possible to insert functionality at any point in your theme. Most WordPress themes include some of the built-in WordPress hooks by default. For example, most of us are aware of the two most common WordPress hooks: wp_head() and wp_footer(), which generally appear in the theme&#8217;s header and footer sections. [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress&rsquo; powerful action-hook system makes it possible to insert functionality at any point in your theme. Most WordPress themes include some of the built-in WordPress hooks by default. For example, most of us are aware of the two most common WordPress hooks: <code>wp_head()</code> and <code>wp_footer()</code>, which generally appear in the theme&rsquo;s header and footer sections. These two hooks provide WordPress a location at which to execute various scripts and functions. For example, the <code>wp_head()</code> hook is where WordPress generates a variety of <code>&lt;link&gt;</code> and <code>&lt;script&gt;</code> elements, <a href="http://digwp.com/2009/06/xmlrpc-php-security/" title="The xmlrpc.php File and Site Security">among other things</a>.</p>
<p><span id="more-744"></span></p>
<h3>See hooks in action</h3>
<p>To see this functionality in action, check out the source code of one of your web pages when the <code>wp_head()</code> hook is present in your <code>header.php</code> file (as it should be by default). Then remove the hook, reload the page, and check the source again. You will see that it is the <code>wp_head()</code> hook that enables those markup-generating functions to operate as intended.</p>
<h3>Getting precise with custom hooks</h3>
<p>In a similar vein, other areas of your theme may also be targeted with WordPress hooks. We have already mentioned the <code>wp_footer()</code> hook, which is useful for <a href="http://digwp.com/2009/06/including-jquery-in-wordpress-the-right-way/" title="Including jQuery in WordPress (The Right Way)">adding scripts</a>, <a href="http://digwp.com/2009/05/dynamic-copyright-in-your-wordpress-footer/" title="Dynamic Copyright in your WordPress Footer">copyright information</a>, and <a href="http://digwp.com/2009/08/show-off-your-wordpress-database-statistics/" title="Show Off Your WordPress Database Statistics">database statistics</a> to the bottom of your pages. Indeed, you can apply targeted functionality to any of the <a href="http://codex.wordpress.org/Plugin_API/Action_Reference" title="WordPress Codex: Plugin API/Action Reference">currently available WordPress hooks</a>, but many times it&rsquo;s just as easy to create your own hook and place it exactly where you need it. Whether you are writing a plugin or implementing some custom <code>functions.php</code> goodness, <strong>custom hooks enable precision targeting anywhere within your theme</strong>, making it more flexible and easier to customize. Let&rsquo;s look at an example..</p>
<h3>An example of how custom hooks are used</h3>
<p>At <a href="http://perishablepress.com/" title="Perishable Press: Digital Design and Dialogue">Perishable Press</a>, I provide my guests with nearly twenty alternate themes that may used while visiting the site. Each of these themes is visually and functionally unique, requiring different sets of plugins and custom scripts in order to function properly. To get the plugins to play nice together and target only select groups of themes, I take advantage of WordPress&rsquo; custom-hook functionality. For example, I have a stats plugin that needs to hook into <code>wp_head()</code> for a few of my themes, but using that specific hook would result in conflicting scripts in some of the other themes. Thus, I added a custom hook in the header of the themes that require the stat plugin&rsquo;s functionality. Other themes are not affected and everything clicks along like clockwork.</p>
<h3>Creating and using custom hooks</h3>
<p>Creating and using custom hooks is surprisingly simple. You basically need to define the hook in your <code>functions.php</code> (or plugin) file, and then place the hook tag in the desired location within your theme. Thus:</p>
<p><strong>Step 1: Define your custom hook</strong></p>
<p>Add this to your <code>functions.php</code> file:</p>
<pre><code>// i can has custom hook
function custom_hook() {
	do_action('custom_hook');
}</code></pre>
<p>This will add an action named &ldquo;custom_hook&rdquo; to WordPress&rsquo; hook library, enabling you to use it anywhere in your theme template. Of course, you should name your custom hook something that makes sense for your particular application. Once that is in place, move on to the next step.</p>
<p><strong>Step 2: Tag your custom hook</strong></p>
<p>Place the function call in the desired location within your theme template:</p>
<pre><code>&lt;?php custom_hook(); ?&gt;</code></pre>
<p>That&rsquo;s all there is to it. With those two snippets of code in place, your theme is equipped with a custom hook through which you may execute any function you wish using WordPress&rsquo; powerful <code>add_action()</code> feature.</p>
<p><strong>Step 3: Add your function</strong></p>
<p>To keep things simple, we&rsquo;ll use a very basic function to illustrate this step. In your <code>functions.php</code> file, add the following code:</p>
<pre><code>&lt;?php 
function hello_wordpress() {
	echo '&lt;h1&gt;Hello WordPress!&lt;/h1&gt;';
}
add_action('custom_hook', 'hello_wordpress', 7);
?&gt;</code></pre>
<p>This function will echo &ldquo;Hello WordPress!&rdquo; on your web page at the location of your <code>custom_hook()</code> in your theme template. To really get a sense of how WordPress hooks work, try moving the <code>&lt;?php</code>&nbsp;<code>custom_hook();</code>&nbsp;<code>?&gt;</code> tag to a different location within your theme and watch the output text move accordingly. Amazing, but true.</p>
<p>The key to using your custom hook for one of your functions is the following line:</p>
<p><code>add_action('custom_hook', 'hello_wordpress', 7);</code></p>
<p>This is where you actually associate your function with the custom hook. The first parameter is the name of the custom hook, the second parameter is the name of the function, and the third parameter sets the priority of the function. When multiple functions are targeting the same hook, the priority specifies the order in which the action should be performed. The default priority value is &ldquo;<code>10</code>&rdquo;.</p>
<h3>Take-home message</h3>
<p>If nothing else, just remember that WordPress action hooks provide a way for you to output content virtually <em>anywhere</em> in your theme. Chances are that there is already a default hook that will do the job, but if not, the information in this tutorial will enable you to create and use custom hooks as needed to get the job done.</p>
<p>That does it for this fine <acronym title="Digging into WordPress">DiW</acronym> tutorial. Be sure to <a href="http://feeds2.feedburner.com/DiggingIntoWordpress" title="Grab the DiW Feed!">subscribe to our feed</a> for more great WordPress content delivered fresh to your feed reader!</p>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/09/wordpress-action-hooks/">Permalink</a> | <a href="http://digwp.com/2009/09/wordpress-action-hooks/#comments">11 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/09/wordpress-action-hooks/&title=Precision Targeting with Custom Action Hooks">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/actions/" rel="tag">actions</a>, <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/hooks/" rel="tag">hooks</a>, <a href="http://digwp.com/tag/plugin/" rel="tag">plugin</a>, <a href="http://digwp.com/tag/theme/" rel="tag">Theme</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/09/wordpress-action-hooks/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

