If someone try to open a private page as guest user he says only "Oops! That page can’t be found.", I want to redirect it to a exact page, I try to do it in functions.php but I'm confused what to add as $tag in add_action() and how to do this redirect?
add_action( 'WHAT_TO_ADD', 'private_post_redirect' );
function private_post_redirect()
{
global $ID;
if (!is_user_logged_in() ) :
if ( get_post_status ( $ID ) == 'private' ) :
echo '<h1>private - functions.php</h1>';
else :
echo '<h1>public - functions.php</h1>';
//wp_redirect( '/login/' ); exit;
endif;
endif;
}
If someone try to open a private page as guest user he says only "Oops! That page can’t be found.", I want to redirect it to a exact page, I try to do it in functions.php but I'm confused what to add as $tag in add_action() and how to do this redirect?
add_action( 'WHAT_TO_ADD', 'private_post_redirect' );
function private_post_redirect()
{
global $ID;
if (!is_user_logged_in() ) :
if ( get_post_status ( $ID ) == 'private' ) :
echo '<h1>private - functions.php</h1>';
else :
echo '<h1>public - functions.php</h1>';
//wp_redirect( '/login/' ); exit;
endif;
endif;
}
Share
Improve this question
asked Jan 20, 2016 at 11:28
jExchangejExchange
254 bronze badges
3
|
1 Answer
Reset to default 0The WordPress Global You should try this:
function redirect_private_content() {
global $wp_query, $wpdb;
if ( is_404() ) {
$current_query = $wpdb->get_row($wp_query->request);
if( 'private' == $current_query->post_status ) {
wp_redirect( home_url('/') );
exit;
}
}
}
add_action( 'template_redirect', 'redirect_private_content', 9 );
template_redict
=== "WHAT_TO_ADD" – Pieter Goosen Commented Jan 20, 2016 at 12:06