Using WordPress ‘init’ PHP action

The init WordPress PHP action fires after WordPress has finished loading but before any headers are sent. It’s commonly used to intercept $_GET or $_POST data and load plugin text domains.

Usage

add_action('init', 'your_custom_function');
function your_custom_function() {
    // your custom code here
}

Parameters

  • No parameters.

More information

See WordPress Developer Resources: init

  • Use init to act on $_POST or $_GET data.
  • Call load_plugin_textdomain during init.
  • If you want to plug an action once WP is loaded, use the wp_loaded hook.

Examples

Process POST data

Process submitted form data using $_POST.

add_action('init', 'process_post_data');
function process_post_data() {
    if (isset($_POST['unique_hidden_field'])) {
        // process $_POST data here
    }
}

Load plugin text domain

Load the plugin’s text domain for localization.

add_action('init', 'load_my_plugin_textdomain');
function load_my_plugin_textdomain() {
    load_plugin_textdomain('my-plugin', false, basename(dirname(__FILE__)) . '/languages');
}

Register custom post type

Register a custom post type during the init action.

add_action('init', 'register_my_custom_post_type');
function register_my_custom_post_type() {
    // register custom post type 'book' here
}

Add rewrite rules

Add custom rewrite rules for custom URLs.

add_action('init', 'add_my_rewrite_rules');
function add_my_rewrite_rules() {
    // add custom rewrite rules here
}

Register custom taxonomies

Register custom taxonomies for your custom post types.

add_action('init', 'register_my_custom_taxonomies');
function register_my_custom_taxonomies() {
    // register custom taxonomies here
}