WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Action Reference/wp

This page is marked as incomplete. You can help Codex by expanding it.

Description

This action hook runs immediately after the global WP class object is set up. The $wp object is passed to the hooked function as a reference (no return is necessary).

This hook is one effective place to perform any high-level filtering or validation, following queries, but before WordPress does any routing, processing, or handling. It is run in the main() WP method in which the $query_args are passed to parse_request(), as well as when send_headers() , query_posts(), handle_404(), and register_globals() are setup.

Examples

This action will allow us to set a cookie in the headers but still have access to the WP class object. This example will show you how to set a cookie if you are on a specific page ID.

function set_cookie() {
// using is_page conditional for specific page ID
  if(is_page(126)){
    setcookie ("my-custom-cookie", "true", time()+60*60*24*365*5, COOKIEPATH, COOKIE_DOMAIN, false); 
  }
}

add_action( 'wp', 'set_cookie' );

Source File

The 'wp' hook is found in /wp-includes/class-wp.php, within the main() method of the WP() class.