Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use admin_init filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 12, 2022
5 minutes read

admin_init filter

Note, this does not just run on user-facing admin screens. It runs on admin-ajax.php and admin-post.php as well.

To use admin_init filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

We at Flipper Code, always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function modify_admin_init_defaults and we registered using add_filter. The first parameter admin_init is name of the hook, The second parameter modify_admin_init_defaults is name of the function which need to be called, third parameter is the priority of calling the hook if same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometime, you have to remove a registered hook so you can use remove_filter to remove admin_init filter.

Parameters

  • No parameters

Live Example

/**
 * Restrict access to the administration screens.
 *
 * Only administrators will be allowed to access the admin screens,
 * all other users will be shown a message instead.
 *
 * We do allow access for Ajax requests though, since these may be
 * initiated from the front end of the site by non-admin users.
 */
function restrict_admin() {
 ) );

Below is an example how you can use this hook.

                        function modify_admin_init_defaults() { 
   
                            // Update the $viewport_meta variable according to your website requirements and return this variable. You can modify the $viewport_meta variable conditionally too if you want.

                            return $viewport_meta; 
                        }
                        // add the filter
                        add_filter( "admin_init", "modify_admin_init_defaults");

To remove a hook callback, use the example below.

remove_filter( "admin_init", "modify_admin_init_defaults");

Please make sure provide the same callback function name, priority and number of arguments while removing the hook callback.

Flipper Code is a Premium WordPress Plugins development company and integrating new functionalites into WordPress sites in form of custom WordPress Plugins since 2012. If you’re having any trouble using this hook, please contact our WordPress Development Team and we’d be happy to assist you.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.