Redirects To Another Page In WordPress Backend
January 21st, 2010 by Frank • WordPress Hacks • 2 Comments
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' );
Info
- Published in WordPress Hacks
- Tags: backend, Code, development, PHP, WordPress, WordPress Tutorials, WP
- Comment feed | Trackback URL
- read: 21928 | today: 87
- leave a Comment



I'll Try it..I wish successfully
Regard,
JhezeR
Just found the site. It's going to be a busy weekend reading all the previous posts. Great work thanks.