init
This is a WordPress - init hook. The plugin just uses it.
Fires after WordPress has finished loading but before any HTTP headers are sent.
init is a popular event. Plugins commonly use it to initialize themselves. This hook is useful for many reasons: for example, when the user must already be identified, taxonomies are needed, or functions defined by the theme (functions.php or another file) are required.
By the time init fires, the current user is authenticated (the global $current_user is already defined), and all main WordPress global variables and functions are set up. Theme functions are also loaded, and settings such as thumbnail sizes and menu support have been registered.
You can use wp_loaded instead of init; the events are nearly identical. wp_loaded fires immediately after init and after ms_site_check() checks the current blog in a multisite installation. wp_loaded does not fire if the current blog fails its operational check (it is inactive, deleted, marked as spam, or archived).
Events that fire before init:
muplugins_loaded registered_taxonomy registered_post_type plugins_loaded sanitize_comment_cookies setup_theme load_textdomain after_setup_theme auth_cookie_malformed auth_cookie_valid set_current_user init
Usage
add_action( 'init', 'wp_kama_init_action' );
/**
* Function for `init` action-hook.
*
* @return void
*/
function wp_kama_init_action(){
// action...
}
Examples
#1 Attach to other events from init
add_action('init','all_my_hooks');
function all_my_hooks(){
// Attach handlers to later events.
add_action('admin_init', 'my_function_name');
add_action('admin_menu', 'my_function_name');
}
#2 Handle a $_GET request
Suppose we need to redirect to the registration page when the request contains the register variable:
add_action('init', 'redirect_to_register');
function redirect_to_register(){
if( isset( $_GET['register'] ) ) {
wp_redirect( site_url() . 'wp-register.php');
exit;
}
}Where the hook is called
do_action( 'init' );
Where the hook is used in WP CLI
remove_action( 'init', 'wp_cron' );
add_action( 'init', 'kses_remove_filters', 11 );