The admin_init WordPress PHP action fires as an admin screen or script is being initialized, and it runs on admin-ajax.php and admin-post.php as well.
Usage
add_action('admin_init', 'your_custom_function'); function your_custom_function() { // your custom code here }
Parameters
This action does not provide any parameters.
More information
See WordPress Developer Resources: admin_init
Examples
Redirect non-admin users
Prevent non-admin users from accessing the WordPress admin area by redirecting them to the homepage.
add_action('admin_init', 'redirect_non_admin_users'); function redirect_non_admin_users() { if (!current_user_can('manage_options')) { wp_redirect(home_url()); exit; } }
Add custom CSS to admin area
Add custom CSS to the WordPress admin area.
add_action('admin_init', 'add_custom_admin_css'); function add_custom_admin_css() { wp_enqueue_style('custom_admin_css', get_template_directory_uri() . '/admin-style.css'); }
Add custom settings section
Create a custom settings section in the WordPress admin area.
add_action('admin_init', 'register_custom_settings'); function register_custom_settings() { register_setting('your_settings_group', 'your_setting'); add_settings_section('your_section_id', 'Your Section Title', 'your_section_callback', 'your_page_slug'); add_settings_field('your_setting_id', 'Your Setting Label', 'your_setting_callback', 'your_page_slug', 'your_section_id'); }
Limit revisions
Limit the number of revisions stored for each post.
add_action('admin_init', 'limit_post_revisions'); function limit_post_revisions() { if (!defined('WP_POST_REVISIONS')) { define('WP_POST_REVISIONS', 5); } }
Disable admin bar for non-admin users
Remove the admin bar for non-admin users.
add_action('admin_init', 'disable_admin_bar_for_non_admins'); function disable_admin_bar_for_non_admins() { if (!current_user_can('manage_options')) { show_admin_bar(false); } }