0

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?

  • where are you setting $pagenow ? – David Lee Oct 24 '17 at 20:10
  • Just to make sure i understood: 1) you want something like admin.php?page=example-options to redirect users to admin.php?page=theme_options 2) will there be any kind of role condition, i.e. non-admin users are redirected but admins can access the page ? – Celso Bessa Oct 24 '17 at 22:51
  • @CelsoBessa Yes, I want to be able to do something like redirect something like admin.php?page=example-options to another page, as opposed to redirecting from something like customize.php to another page. I already came up with the role condition but omitted it from this code as it is not relevant to the core of the problem I am having. – user2461674 Oct 25 '17 at 1:58
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.