Skip to content

Instantly share code, notes, and snippets.

@rfmeier
Last active December 16, 2015 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfmeier/5486273 to your computer and use it in GitHub Desktop.
Save rfmeier/5486273 to your computer and use it in GitHub Desktop.
Redirect a to a specific page depending on whether the user is authenticated or not.
<?php
/**
* Callback for WordPress 'template_redirect' action.
*
* http://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect
*
* @return none
*/
function my_custom_redirect(){
// if not on specific page, continue to page (use is_single() to detect a specific post)
if( ! is_page( 10 ) )
return;
// if the user is logged in, continue to page
if( is_user_logged_in() )
return;
// redirect to page id 5
wp_redirect( get_permalink( 5 ) );
// (optional) prevent other action callbacks from performing addtional redirects
exit();
}
add_action( 'template_redirect', 'my_custom_redirect' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment