最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Redirect from "private" page and functions.php which tag add to add_action()?

programmeradmin2浏览0评论

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
  • template_redict === "WHAT_TO_ADD" – Pieter Goosen Commented Jan 20, 2016 at 12:06
  • @PieterGoosen if I add this then is no one "echo" option as output visible but also no redirect... – jExchange Commented Jan 20, 2016 at 12:13
  • There are plenty of info on this site. Be sure to use the site search. Also, where is $ID coming from – Pieter Goosen Commented Jan 20, 2016 at 12:52
Add a comment  | 

1 Answer 1

Reset to default 0

The 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 );
发布评论

评论列表(0)

  1. 暂无评论