wordpress

Redirects To Another Page In WordPress Backend

WordPress allows using a function, to simple redirect to a URL, the function wp_redirect() enables to specify an address and a status.

This makes it easy to realize a forward even in the backend of WordPress. Two different examples will illustrate this. The difference lies in the query of the URL, which will be checked.

/**
 * Redirects to another page, with a workaround for the IIS Set-Cookie bug.
 *
 * @link http://support.microsoft.com/kb/q176113/
 * @since 1.5.1
 * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status.
 *
 * @param string $location The path to redirect to
 * @param int $status Status code to use
 * @return bool False if $location is not set
 */
function wp_redirect($location, $status = 302) 

In our first example, we use the content of the variable $pagenow, which always contains the page name and so allows a clean check. In both functions, the function admin_url() is queried, because that is only since version 2.6 in the core of WordPress. Depending on the version, you can save this query and also there are other functions of this kind, which facilitate the setting of the address, see this Post.

function fb_redirect_1() {
	global $pagenow;
	
	if ( 'plugins.php' === $pagenow ) {
		if ( function_exists('admin_url') ) {
			wp_redirect( admin_url('edit-comments.php') );
		} else {
			wp_redirect( get_option('siteurl') . '/wp-admin/' . 'edit-comments.php' );
		}
	}
}
if ( is_admin() )
	add_action( 'admin_menu', 'fb_redirect_1' );

The second option is a little different and checks on the URL to the global variable $ _SERVER and thereby the key REQUEST_URI. This is useful for example when querying the dashboard in the admin area, because this is not always given back with a value of $pagenow, means you reach the Dashbaord partly via index.php, or directly via wp-admin/.

function fb_redirect_2() {
	
	if ( preg_match('#wp-admin/?(index.php)?$#', $_SERVER['REQUEST_URI']) ) { 
		if ( function_exists('admin_url') ) {
			wp_redirect( admin_url('edit-comments.php') );
		} else {
			wp_redirect( get_option('siteurl') . '/wp-admin/' . 'edit-comments.php' );
		}
	}
}
if ( is_admin() )
	add_action( 'admin_menu', 'fb_redirect_2' );

Comments are closed.

7 comments

  1. JhezeR

    I'll Try it..I wish successfully
    Regard,
    JhezeR

  2. Gareth

    Just found the site. It's going to be a busy weekend reading all the previous posts. Great work thanks.

  3. TED

    Hi,

    I'm trying to redirect the home page of my blog to the last post permalink using wp_redirect function.

    Do you have any suggestions?

  4. Ann

    I can tell this is exactly what i need... with one exception... the if is_admin line. I want it to be if ( current_user_can('edit_posts') but if I make that change the whole thing is just ignored..

    Any suggestions?

  5. ddlg2007

    Thanks Frank you save my live GG.I have searched many hours something like that, not really imagine how much you just help me. I use the second option to redirect to wp-admin profile.php (customized). Just 1 question:
    This never fails? I want nobody to access wp-admin wordpress or desk but can see the profile as I do now.

  6. Frank

    @ddlg2007: i think, works without fails - but you must check before WP update, maybe WP has change the function.

  7. ddlg2007

    Ok thanks a lot Frank, for the momento is wordking perfect. You are great man, thankssssssssssss.