Skip navigation

Tag Archives: development

What’s the difference between add_filter and add_action?

The difference is semantic. You can use add_filter() for an action, and you can use add_action() for a filter. The difference is there because actions and filters have a different code “mentality.” Filters are just that, filters through which information is passed, and then used. For example… the contents of this entry are passed through several filters. They turn my line breaks into HTML paragraphs, and correct HTML mistakes. Most importantly, when all filters have been run, something is done with that information. With actions, information may be passed between functions that use the action hook, but nothing is done with the information after all the filters have run. An example would be when I posted this entry publish_post was fired, and passed along the ID of this post. Plugins could use that ID to do things related to this entry, and pass on the ID for other plugins, but after all the plugins have run, the information passed along (the ID) is thrown away. Actions also have the possibility of not passing on any information (“No Parameters” hooks). These are hooks that are merely placeholders… like a template header or an admin footer. These are the only hooks whose functions should not return any information. Important note: actions are not all “No Parameters” hooks! It is still important to return any information you may get when using an action hook… even though WordPress won’t be using it, other plugins might!

query_vars

query_vars is a filter on the query variables used by the WP_Query class (and stored in $wp_request->public_query_vars).

The initial array consists of the following query variables:

  • m
  • p
  • posts
  • w
  • cat
  • withcomments
  • s
  • search
  • exact
  • sentence
  • debug
  • calendar
  • page
  • paged
  • more
  • tb
  • pb
  • author
  • order
  • orderby
  • year
  • monthnum
  • day
  • hour
  • minute
  • second
  • name
  • category_name
  • feed
  • author_name
  • static
  • pagename
  • page_id
  • error
  • comments_popup
  • attachment
  • attachment_id
  • subpost
  • subpost_id
  • preview

Your hook would accept an array, add an item to the array, and return the array.

function add_my_query_var($vars) {
$vars[] = 'my_var';
return $vars;
}

add_filter('query_vars', 'add_my_query_var');
Follow

Get every new post delivered to your Inbox.