WordPress Function: add_action() Action Hook Priority


Any number of functions may be "hooked" to any given action. In some instances it is important for a hooked function to execute before or after others, which is where the third parameter to add_action(), $priority comes into play.

If the $priority argument is omitted, the function will be attached with the default priority of 10. When the action is "triggered", the "hooked" functions will be called starting with those added with the smallest $priority, and progressing to the functions with the largest $priority. Any hooked functions that share the same priority will be called in the order that they were added (the order in which their respective add_action() calls were executed).

For instance, say a third-party plugin is using a function hooked to the 'template_redirect' action in order to forward visitors to the daily-deal page to an affiliate link for an external e-commerce site, but you'd like the redirection to only occur for logged-in users. You would need to use your own 'template_redirect' hook to send logged-out visitors to the sign-in page. After determining that the third-party plugin attaches it's function with the default $piority of 10, you could hook your function with a priority of 9 to ensure that your logged-in check happens first:

function redirect_deal_visitors_to_login() {
  if( is_page( 'daily-deal' ) && !user_is_logged_in() ) {
    wp_redirect( wp_login_url() );

add_action( 'template_redirect', 'redirect_deal_visitors_to_login', 9 );