init

Home » Hooks » init

The "init" hook is one of the earliest actions that get executed in the WordPress loading process. It’s an important hook that’s used to initialize various aspects of WordPress, such as loading text domains, registering custom post types, and setting up rewrite rules.

The "init" hook is used to execute code that needs to be run before the template is loaded. For instance, if you need to add a custom post type or taxonomy to your website, you can use the "init" hook to register them. Additionally, you can also use this hook to add filters to the default WordPress queries, allowing you to modify or filter the data that’s returned by WordPress.

Here’s a sample code snippet that demonstrates how to use the "init" hook to register a custom post type:

function custom_post_type() {
    $args = array(
        'public' => true,
        'label'  => 'Custom Post Type'
    );
    register_post_type( 'custom_post_type', $args );
}
add_action( 'init', 'custom_post_type' );

In this example, we’re defining a custom post type called "Custom Post Type" and registering it using the "register_post_type" function. We’re also attaching this function to the "init" hook using the "add_action" function, which ensures that it runs when WordPress is initializing.

By using the "init" hook, you can ensure that your customizations are loaded early in the WordPress loading process, allowing you to modify WordPress behavior in a more efficient and streamlined manner.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.