Top

HAMMADHASSAN on "Redirects To Another Page In WordPress Backend"

February 14, 2010 by David Olsen 

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

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

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • NewsVine
  • Reddit
  • StumbleUpon
  • Google Bookmarks
  • Yahoo! Buzz
  • Twitter
  • Technorati
  • Live
  • LinkedIn
  • MySpace
  • Share/Bookmark

Related posts:

  1. JordashTalon on "An Edit Page AND Edit Post Link" I want to put both an Edit Page and Edit...
  2. t31os_ on "How to restrict comment-ability on a per-user/per-page basis?" Have you checked the function reference. http://codex.wordpress.org/Function_Reference Action and filter...
  3. HAMMADHASSAN on "Monetize Our WordPress Blog" How can We do to monetize Our wordpress blog. Share...
  4. zguy on "Blog not showing – errors in theme page" blog address: Running: - OS: Windows Standard Server 2003 -...
  5. iLucato on "Ideas for WordPress by Lucato" Hey Ipstenu, thanks a lot for your time and reply....

Related posts brought to you by Yet Another Related Posts Plugin.

Comments

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!





Security Code:

Bottom