php - Redirect User when they click Menu Option

I am setting up a new website where I am managing a small amount of clients. I have created a menu page that when the us

I am setting up a new website where I am managing a small amount of clients. I have created a menu page that when the user is logged out it basically just tells the user to login. But I want to code for when a user is logged in and clicks on that same menu page, I want it to redirect to a private page I have created for them. Each individual already has a private page. The URL for each private page is "example/private-page/username/. If the URL doesn't exist for an individual I just want the code to go back to the original menu page not a nonexistent page.

I have tried different ways including using meta to refresh the page to the other url. I can't figure what exactly isn't working. I am typing the code on the function.php

 function get_page_by_slug( $slug ) {
        if( $pages = get_pages() )
            foreach( $pages as $page )
                if( $slug === $page->post_name ) return true;
                return false;
   } 

   function userredirect() {
        $current_user = wp_get_current_user();
        $slug = $current_user->user_login;
        if( is_user_logged_in() && is_page('Menu Page') ){
             if( get_page_by_slug($slug) ){
             }wp_redirect('/'.$slug.'/');
                 exit;
        }
    }

Nothing Happens when I go to the menu page besides it showing the original content for the page.

I am setting up a new website where I am managing a small amount of clients. I have created a menu page that when the user is logged out it basically just tells the user to login. But I want to code for when a user is logged in and clicks on that same menu page, I want it to redirect to a private page I have created for them. Each individual already has a private page. The URL for each private page is "example/private-page/username/. If the URL doesn't exist for an individual I just want the code to go back to the original menu page not a nonexistent page.

I have tried different ways including using meta to refresh the page to the other url. I can't figure what exactly isn't working. I am typing the code on the function.php

 function get_page_by_slug( $slug ) {
        if( $pages = get_pages() )
            foreach( $pages as $page )
                if( $slug === $page->post_name ) return true;
                return false;
   } 

   function userredirect() {
        $current_user = wp_get_current_user();
        $slug = $current_user->user_login;
        if( is_user_logged_in() && is_page('Menu Page') ){
             if( get_page_by_slug($slug) ){
             }wp_redirect('https://example/private-page/'.$slug.'/');
                 exit;
        }
    }

Nothing Happens when I go to the menu page besides it showing the original content for the page.

Share Improve this question asked Aug 11, 2019 at 19:06 Jacob RoweJacob Rowe 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

The first problem I see is here:

if( get_page_by_slug($slug) ){
}wp_redirect('https://example/private-page/'.$slug.'/');
exit;

The if() statement will do nothing; your wp_redirect() and exit statements are outside the {} braces.

Try this instead:

 if( get_page_by_slug($slug) ){
    wp_redirect('https://example/private-page/'.$slug.'/');
    exit;
 }

Aside from that, a couple other things to check:

  • You say the code is in function.php; if you're referring to a theme file, it needs to be functions.php (note the s) in the currently-active theme.
  • Where are these functions being called from? What action/filter hook are you adding them to?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745250846a4618668.html

相关推荐

  • php - Redirect User when they click Menu Option

    I am setting up a new website where I am managing a small amount of clients. I have created a menu page that when the us

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信