Feature
Post

Category
Code


WordPress Plugin Actions

How To Write a WordPress Plugin Series

This post was written as part of the How to Write a WordPress Plugin series.

WordPress actions allow you as a plugin author to be able to hook into the WordPress application and execute a piece of code. An example of an action would be that you want a execute some code after a user has published a post or left a comment.

Some of the actions that I use heavily are:

  • admin_menu: Allows you to set up an admin panel for your plugin.
  • wp_head: Allows you to insert code into the <head> tag of a blog

Actions in Action

While defining the structure of a WordPress plugin, I left a place holder for some actions. In this example, we are going to set up a piece of code that will run inside the <head> tag of a WordPress blog.

First we need to add a function into our DevloungePluginSeries class.

PHP:
  1. function addHeaderCode() {
  2.             ?>
  3. <!-- Devlounge Was Here -->
  4.             <?php
  5.        
  6.         }

All the above function does is output an HTML comment. Rather simple, but you could output just about anything. To call this function, we add an action.

PHP:
  1. //Actions and Filters   
  2. if (isset($dl_pluginSeries)) {
  3.     //Actions
  4.     add_action('wp_head', array(&$dl_pluginSeries, 'addHeaderCode'), 1);
  5.     //Filters
  6. }

From the WordPress Plugin API page, the add_action structure is as follows:
add_action ( 'hook_name', 'your_function_name', [priority], [accepted_args] );

Since we are calling a function inside of a class, we pass the action an array with a reference to our class variable (dl_pluginSeries) and the function name we wish to call (addHeaderCode). We have given our plugin a priority level of 1, with lower numbers executed first.

Running the Code

If the Devlounge Plugin Series plugin is activated, the comment of "Devlounge was here" should show up when you go to View->Source in your web browser when looking at your main blog site.

Removing Actions

If your plugin dynamically adds actions, you can dynamically remove actions as well with the remove_actions function. The structure is as follows:
remove_action('action_hook','action_function').

Conclusion

Hopefully this post gave you some insight into what you could do when hooking into WordPress actions. Once again, you can download the code for the sample Devlounge Plugin Series for actions.

Download the Code Used In This Post

For further reading, please check out these links:


  1. By Ronald Huereca posted on May 13, 2007 at 8:53 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    AJ,

    One of the readers was curious what plugin you used for showing code. Could you please leave a comment here with the link to the plugin?

  2. By aj posted on May 14, 2007 at 5:35 am
    Want an avatar? Get a gravatar! • You can link to this comment

    The plugin is iG Syntax hilighter

  3. By Ian posted on May 31, 2008 at 2:11 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Just a tip for anyone who ran into this. The wp_head action wouldn’t work for me at first. Turns out I forgot to add <?php wp_head(); ?> between my html head tags for my theme, so remember to check your theme header template if it doesn’t work for you, all is well now.

    Fixed post, the first one stripped out the php.

  4. By Epic Alex posted on June 10, 2008 at 7:11 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Hey, thanks for this series, this is the second time that I’ve referred to it to help me write a plugin. I have come across a problem though. I have a function that test is_single and also if a value in my array is equal to true. This function then hooks into wp_head. However it doesn’t work. I think I might need to make my option variable global, to be able to use it like this? Can you point me in the right direction?

    Thanks

  5. By Bob posted on September 19, 2008 at 9:48 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    http://vofece9264.justfree.com/and9597.html and
    http://vofece9264.justfree.com/of5131.html of
    http://vutura9431.justfree.com/of8492.html of
    http://mafyve2798.justfree.com/and6032.html and

  6. By Joy posted on May 24, 2009 at 9:42 am
    Want an avatar? Get a gravatar! • You can link to this comment

    What gives? This will not work for me. I’m running Wp v.2.6.1 I’ve got a call to wp_head() in my theme’s header file, but no matter what I do, it does not add it into the header. Tearing my hair out….

  7. By Joy posted on May 24, 2009 at 9:53 am
    Want an avatar? Get a gravatar! • You can link to this comment

    This appears to be a timing issue. How is wp_head() ever called before a plugin is loaded? According to the call stack, wp_head() has long been executed & written before the web engine gets around to loading up the plugins. So this coding method doesn’t appear to be a way to add css file link to a header. How do you add a plugin’s css file to the main WP header?

  8. By Rob Baker posted on July 16, 2009 at 7:43 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    How would I use a wordpress shortcode to display addHeaderCode() ?

    Thanks?

    Pete

  9. By Tom posted on July 25, 2009 at 12:31 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    I appreciate your effort and the quality of the information you provide. I certainly will folow these recommendations!

  10. By Yogibear posted on August 31, 2009 at 6:59 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Hi all,
    Its great that there is someone here to release some helpful information on creating plugins for WordPress. I got here because i’m learning as well! I noticed that some had problems executing their scripts. Anyway, not sure if you had the same problem as me, but my problem was a classic error:

    In the second class_exists statement, REMEMBER there is no exclamation.
    if ( class_exists(“hunt_order_form”) ) {

    NOT

    if ( !class_exists(“hunt_order_form”) ) {

    Since the class should have been created, you are evaluating to see if the class was created. It was definitely an easy mistake to make if you are like me, typing out the tutorial rather than copy and pasting.

    Anyway, not sure if that helped anybody, but hopefully my 1 hour of testing, helped you!

  11. By Allan Brown posted on September 5, 2009 at 1:21 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Suggestion if you ever update this post: Add a section for “using shortcodes”. I had a difficult time figuring this out on my own. For those of you who may need help with this: Look at the shortcode API on codex. Place the “add_shortcode(‘shortcut name’, ‘shortcut function’);” to the same place you put your add_action and add_filter calls:

    add_action(‘admin_menu’, ‘DevloungePluginSeries_ap’);
    add_action(‘wp_head’, array(&$dl_pluginSeries, ‘addHeaderCode’), 1);
    add_action(‘activate_devlounge-plugin-series/devlounge-plugin-series.php’, array(&$dl_pluginSeries, ‘init’));

    //Filters
    add_filter(‘the_content’, array(&$dl_pluginSeries, ‘addContent’),1);
    add_filter(‘get_comment_author’, array(&$dl_pluginSeries, ‘authorUpperCase’));

    //Shortcodes
    add_shortcode(‘shortcodename’, array(&$dl_pluginSeries, ‘function’));

  12. TrackbackHowTos zu Erstellung eines Wordpress Plugins | Bloganbieter.de BlogDevlounge | WordPress Plugin FiltersHot tipp: dodavanje kontakt forme « 3kolone  Kendi wordpress eklentinizi yazın | BLOG KAZANI¿Cómo crear un plugin para Wordpress? - Carrero Bitácora de los Hermanos Carrero, David Carrero Fernández-Baillo y Jaime Carrero Fernández-Baillo.Deep Blue Wallpaper · CSS Style Guides, W3C news and ValidationPlugin für Wordpress Bastelanleitung - D-SIGN Weblog» Como criar um plugin para WordPress Ganha dinheiro online com MUIOMUIO.NET: Internet, Tecnologia e dicas sobre Blogging por Mario AndradeDr-Hamza’s SpacePlugin-Programmierung für Wordpress - CoreBlogWordPress Plugin Filters | Devlounge插件开发全攻略(目录) 于 Becomin' CharlesKwatog Tech » Tutorial for WordPress Plugin WritingWordPress Plugin Actions Devlounge | Wood TV StandEssential Wordpress Plugin Development Resources, Tutorials and Guides : Speckyboy Design Magazine