do_action( 'init' )
- Since
- 1.5.0
Action init fires after WordPress finishes loading, before headers are sent: the standard place to register post types, taxonomies, and rewrites. The current user is already authenticated at this point; hook wp_loaded instead if you need every init callback to have run first.
Description
Most of WP is loaded at this stage, and the user is authenticated. WP continues to load on the 'init' hook that follows (e.g. widgets), and many plugins instantiate themselves on it for all sorts of reasons (e.g. they need a user, a taxonomy, etc.).
If you wish to plug an action once WP is loaded, use the 'wp_loaded' hook below.
Compatibility
- WordPress
- since 1.5.0
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0).
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Register a custom post type
Register post types and taxonomies on init so they exist on every request before queries and rewrite matching run.
add_action( 'init', 'myplugin_register_book_post_type' );
function myplugin_register_book_post_type() {
register_post_type(
'book',
array(
'labels' => array(
'name' => __( 'Books', 'myplugin' ),
'singular_name' => __( 'Book', 'myplugin' ),
),
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
)
);
}After adding a post type, flush permalinks once (re-save Settings > Permalinks, or flush_rewrite_rules() in an activation hook), otherwise its archive and single URLs 404. Never flush on every init run; it is expensive.
Where this hook fires · 1
wp-settings.php:704file scope
Source code
* themselves on it for all sorts of reasons (e.g. they need a user, a taxonomy, etc.). * * If you wish to plug an action once WP is loaded, use the {@see 'wp_loaded'} hook below. * * @since 1.5.0 */do_action( 'init' ); // Check site status.if ( is_multisite() ) { $file = ms_site_check(); if ( true !== $file ) { require $file;Changelog
Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
- Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.