How to redirect one admin URL to another when admin page URL has "/admin.php?page=" in it?

The following code (from here, middle section chopped out) allows me to redirect users who land on a certain page in the WP admin to another admin page...


function admin_redirects() {
    global $pagenow;

    /* Redirect Customizer to Theme options */
    if($pagenow == 'customize.php'){
        wp_redirect(admin_url('/admin.php?page=theme_options', 'http'), 301);
        exit;
    }

}

add_action('admin_init', 'admin_redirects');

It works great...for some pages. The problem is I cannot get redirects to work for pages originating with a URL structure such as "admin.php?page=example-options." The example-options string is just a plugin I was working on, but I think you get the idea. I have tried a lot of variations and other things at this point, but nothing is working. Am I missing something?

Topic wp-redirect admin-menu Wordpress

Category Web


I think you will achieve what you want by using [get_current_screen()][1] and reading the properties from WP_Screen object returned as needed. In your case, I think you will need $screen->base or maybe $screen->id. According to WordPress Codex:

id (string) The unique ID of the screen

and

base (string) The base type of the screen. For example, for a page 'post-new.php' the base is 'post'.

As an examples, the Yoast SEO Dashboard resides on admin.php?page=wpseo_dashboard and the $screen->base property for this page is 'toplevel_page_wpseo_dashboard'

Below you will find an example taken from something i worked some weeks time ago.

/**
 * Disallow Admin pages for non-superadmin users on current_screen hook.
 * A fallback for when restricting access is not possible by manage_admin_access_on_init
 *
 * @since    0.1.0
 */
public function wpse_283875_manage_admin_access_on_screen() {
    // a list of restricted pages base properties from get_current_screen
    $restricted_screens = array(
        'formularios_page_gf_help',
        'toplevel_page_genesis',
        'genesis_page_genesis-translations',
        'genesis_page_genesis-import-export',
        'genesis_page_genesis-simple-edits',
        'media_page_wp-smush-bulk',
    );
    // get current screen
    $screen = get_current_screen();
    // if there's no $screen object, we are in customizer, bail out
    if ( is_null($screen) ){
        return;
    }
    // get screen->base
    $screen_base = $screen->base;

    // compare screen->base with each restricted screen
    foreach ($restricted_screens as $restricted_screen) {


        // if user can't manage network and screen base is restricted
        if (!current_user_can('manage_network') && $screen_base == $restricted_screen) {
            //get out!
            wp_redirect(admin_url());
            // stop execution
            exit;
        }

    }
}
// hook to current_screen action with priority 5 to execut ASAP
add_action('current_screen','manage_admin_access_on_screen', 5);

If you need to check the screen object, you could use something like this in development environment and read the HTML source:

function debug_admin_screen(){
    $screen = get_current_screen();
    echo '<!--<pre>' . print_r($screen, TRUE) . '</pre>-->';
}
add_action('current_screen', 'debug_admin_screen', 100);

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.