wordpress

Define Your Custom Hook


Define Your Custom Hook

Now it’s time for our plugin scripts. We are going to call them from the footer but we are not going to use wp_footer. We are going to define our own hook!

Just below our first function, place this code:

  1. //My own footer hook
  2. function my_footer() {
  3.     do_action(‘my_footer’);
  4. }

You have now created your own hook and you can add actions to it just like you did in the first function. But before you can do that open up footer.php and position your hook like so:

Note: For demonstration purposes I will remove wp_footer hook from my footer template.

  1. <?php my_footer(); ?>
  2. </body>
  3. </html>

Now that we have a hook, let’s define a function to call our plugin scripts. Add this code in your theme functions file:

  1. //Scripts for Syntax Highlighter plugin in footer section
  2. function syntax_highlighter_scripts()
  3. {
  4.     if (is_single()) { ?>
  5. <script language=”javascript” src=”<?php echo WP_PLUGIN_URL; ?>/google-syntax-highlighter/Scripts/shCore.js” type=”text/javascript”></script>
  6. <script language=”javascript” src=”<?php echo WP_PLUGIN_URL; ?>/google-syntax-highlighter/Scripts/shBrushAll.js” type=”text/javascript”></script>
  7. <script language=”javascript” type=”text/javascript”>
  8. dp.SyntaxHighlighter.ClipboardSwf = ‘<?php echo WP_PLUGIN_URL; ?>/google-syntax-highlighter/Scripts/clipboard.swf’;
  9. dp.SyntaxHighlighter.HighlightAll(‘code’);
  10. </script>
  11. <?php }
  12. }
  13. add_action(‘my_footer’, ‘syntax_highlighter_scripts’, 1);

Just like we did with our first function, this will put the scripts in the custom hook that we defined.

You can also try the same thing with your analytics code.

  1. //Analytics code
  2. function my_analytics()
  3. { ?>
  4. Analytics code here
  5. <?php
  6. }
  7. add_action(‘my_footer’, ‘my_analytics’, 2);

Take note that the third parameter (priority) is 2. This will determine their order of appearance considering that the scripts function is also using the my_footer hook. You can try switching their priority to see what I mean.

Conclusion

Using hooks can greatly improve your theme development process and I urge you to use them so your themes are flexible and easy to maintain. With default hooks in your templates you can insert code without having to open up your template files. You can also define your own custom hooks if you need to.

Having this knowledge on action hooks can also help you in creating your very own theme framework.

You definitely need to check out the following:

If you have something to add regarding this article, your thoughts are always welcome and stay tuned because I will release yet another free WordPress theme framework for the community.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s