繁体   English   中英

在前端登录时将Wordpress用户重定向到他们自己的帖子

[英]Redirect Wordpress User to their own Post on Front-End Login

我正在尝试使Wordpress的前端登录重定向到他们注册时自动创建的帖子(采用自定义帖子类型)。

我可以使用wp_query获得要重定向它们的URL。 我想这不是最好的方法,但是我不了解足够的PHP来解决它。 这是我当前的尝试,但是它只是在空白页面上打印该URL(至少是正确的URL),其登录URL与原来的URL相同:

function my_login_redirect( $redirect_to, $request, $user ){
    global $user, $post;
    $args = array(
       'author' => $current_user->ID,
       'post_type' => 'course-providers',
       'showposts' => 1,
       'caller_get_posts' => 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
       <?php wp_redirect ( the_permalink () ); ?>
       <?php 
    endwhile;
    } else {
        echo "This User Has no Profile";
    }

}
add_filter("login_redirect", "my_login_redirect", 10, 3);

另外,我想我不需要wp_redirect,而应该只使用login_redirect过滤器本身,但是我又一次迷失了,只是在黑暗中拍摄了很多照片。

感谢您的帮助,并让我知道是否还有其他信息可以使此功能对他人更有用或更易于回答。 谢谢!

我最终使用了模板重定向来完成这项工作。 我认为从技术上讲,这是一种更好的方法,但是它加载速度非常快,并且完全可以满足我的需要。

因此,现在,当用户登录时,它会转到直接的url / profiles,并且该页面上的模板只是重定向。 我使用了这个粉碎杂志的帖子中的想法和一些示例代码, 重定向随机重定向

这是我在functions.php文件中使用的用于模板的函数,以使重定向发生:

function profile_redirect() {
// This is a template redirect
// Whenever someone goes to /profile (or any page using the profile template)
// this function gets run

if ( is_user_logged_in() ) {
    global $current_user, $post;
    $args = array(
        'author' => $current_user->ID,
        'post_type' => 'profile',
        'posts_per_page' => 1
        );
    $my_query = null;
    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ) {
        while ( $my_query->have_posts() )
            $my_query->the_post();
            //We have a post! Send them to their profile post.
            wp_redirect ( get_permalink () );
        exit;
    } else {
        // If there are no posts, send them to the homepage
        wp_redirect ( get_bloginfo('url') );
        exit;
    }
    wp_reset_query();
} else {
    // If they're not logged in, send them to the homepage
    wp_redirect ( get_bloginfo('url') );
    exit;
}

}

然后,在我的配置文件模板上,将其放在顶部,并带有php标签以运行该功能:

profile_redirect(); ?>

这对我有用,所以我现在将其保留:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM