Commonly Used Action Hooks – init


The init hook is fired early on in WordPress but after most of WordPress is set up. WordPress itself attach a lot of internal functionality to this hook, such as

  • registration of post types and taxonomies
  • initialization of the default widgets.

Since nearly most of WordPress is setup at this point, your plugin will probably have to use this hook for anything that needs to be done when all the information from WordPress is available, such as, adding the ability for users to write excerpts for pages.

Example on how to use the init action hook

As an example let us see create a simple plugin that registers a custom post type called sc_sdc_question when the init action hook is fired.

Plugin Name: Sytech Digital Classroom
Version: 1.0
Description: Creates custom post type Questions.
Author: Sydney Chako
Author URI:
Plugin URI:

add_action('init', 'sc_register_sdc_questions');

function sc_register_sdc_questions() {
 $labels = array(
 'name' => __( 'Questions', 'wpwa_questions' ),
 'singular_name' => __( 'Question', 'sc_sdc_questions'),
 'add_new' => __( 'Add New', 'sc_sdc_questions'),
 'add_new_item' => __( 'Add New Question', 'sc_sdc_questions'),
 'edit_item' => __( 'Edit Questions', 'sc_sdc_questions'),
 'new_item' => __( 'New Question', 'sc_sdc_questions'),
 'view_item' => __( 'View Question', 'sc_sdc_questions'),
 'search_items' => __( 'Search Questions', 'sc_sdc_questions'),
 'not_found' => __( 'No Questions found', 'sc_sdc_questions'),
 'not_found_in_trash' => __( 'No Questions found in Trash', 'sc_sdc_questions'),
 'parent_item_colon' => __( 'Parent Question:', 'sc_sdc_questions'),
 'menu_name' => __( 'Questions', 'sc_sdc_questions'),
 $args = array(
 'labels' => $labels,
 'hierarchical' => true,
 'description' => __( 'Sytech Digital Classroom Questions and Answers', 'sc_sdc_questions'),
 'supports' => array( 'title', 'editor', 'comments' ),
 'public' => true,
 'show_ui' => true,
 'show_in_menu' => true,
 'show_in_nav_menus' => true,
 'publicly_queryable' => true,
 'exclude_from_search' => false,
 'has_archive' => true,
 'query_var' => true,
 'can_export' => true,
 'rewrite' => true,
 'capability_type' => 'post'
register_post_type( 'sc_sdc_question', $args );


To install the plugin:

  • save the file and upload it to the wp-content/plugins folder on your server using your FTP.
  • Go to your WordPress installed plugins on admin panel. If everything when well, you should now see a menu for creating a question. Plugin

This is the basic code for creating custom post types, and you can play around with it to create you own custom post types. If you have been following our tutorial from the beginning, you should now be familiar with how to work with hooks, and everything else is readable. I hope you can see how we enabled title, editor, and comments in the configuration for question title, question description, and answers.

However, the objective of this example was not necessarily about custom post creation but about how and when to use the init action hook.

Another example on how to use the init action hook

Let us say you want to create a plugin that checks if a user is logged in when WordPress first loads up but before any output is displayed to the browser. Because we want to do the check before anything content is sent to the browser, we target the init hook.


/* add your callback to the init hook */
add_action( 'init', 'my_user_check' );

/* the callback function */

function my_user_check() {
 if ( is_user_logged_in() ) {
 // do something if the user is logged in