最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Admin Page Redirect

programmeradmin1浏览0评论

Is it possible to redirect users to an admin page if they access another admin page?

For example if they a user ever hits "all pages" /wp-admin/edit.php?post_type=page

they would get redirected to "add New page" /wp-admin/post-new.php?post_type=page

Is it possible to redirect users to an admin page if they access another admin page?

For example if they a user ever hits "all pages" /wp-admin/edit.php?post_type=page

they would get redirected to "add New page" /wp-admin/post-new.php?post_type=page

Share Improve this question edited Mar 23, 2018 at 16:08 Michael Ecklund 6,83213 gold badges68 silver badges110 bronze badges asked May 14, 2012 at 19:46 ShaeShae 7413 gold badges9 silver badges19 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 31
/**
 * Redirect admin pages.
 *
 * Redirect specific admin page to another specific admin page.
 *
 * @return void
 * @author Michael Ecklund
 *
 */
function disallowed_admin_pages() {

    global $pagenow;

    # Check current admin page.
    if ( $pagenow == 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'page' ) {

        wp_redirect( admin_url( '/post-new.php?post_type=page' ) );
        exit;

    }

}

Fire the above function on the hook admin_init.

add_action( 'admin_init', 'disallowed_admin_pages' );

Alternate syntax:

/**
 * Redirect admin pages.
 *
 * Redirect specific admin page to another specific admin page.
 *
 * @return void
 * @author Michael Ecklund
 *
 */
add_action( 'admin_init', function () {

    global $pagenow;

    # Check current admin page.
    if ( $pagenow == 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'page' ) {

        wp_redirect( admin_url( '/post-new.php?post_type=page' ) );
        exit;

    }

} );

Michael's solution appears to be intended for use inside a class, so for anyone wanting a standalone function which will work directly in functions.php, the example below includes a redirect from customize.php to a theme options page and the one from Michael's original function.

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

    /* OP's redirect from /wp-admin/edit.php?post_type=page */
    if($pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'page'){
        wp_redirect(admin_url('/post-new.php?post_type=page', 'http'), 301);
        exit;
    }
}

add_action('admin_init', 'admin_redirects');

Yes this is possible by adding an action to admin_init, at that point you could check the request uri to see if it matches /wp-admin/edit.php?post_type=page and if it does issue a redirect to the add posts page: /wp-admin/post-new.php?post_type=page.

Also the Plugin API and the action reference pages on the WordPress codex go into more detail about actions and how they work.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论