前端登录:将用户重定向到他们创建的帖子

时间:2013-02-21 作者:Rob

是否可以将前端登录的用户重定向到他们创建的帖子?

我在前端有下面的表格,它可以很好地帮助人们登录,但并没有达到我想要的效果。

我创建了一个前端注册流程,它创建了一个新用户和一个帖子,并在帖子中填写了某些细节。完成该过程后,他们会被发送到他们刚刚创建的帖子,然后允许他们在前端编辑所有详细信息。这对于第一次注册的人来说非常有效,但对于那些已经注册并想登录的人,我不能完全实现我想要的。

当他们在下面的表格中填写详细信息时,我希望它重定向到他们注册时创建的帖子,这可能吗?

用户只能创建ONE 邮递作为配置文件。所以我希望他们能够登录并重定向到他们的个人资料。

如果有帮助,用户名与帖子标题相同(用户名是个人公司名称,也是帖子名称)。

            <?php if (!(current_user_can(\'level_0\'))){ ?>   
                <form action="<?php echo get_option(\'home\'); ?>/wp-login.php" method="post" id="login-form">
                    <p style="color:black!IMPORTANT;">Please login.</p>
                    <!--[if !(IE)]><!-->
                        <input type="text" placeholder="Username" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" />
                    <!--<![endif]-->
                    <!--[if (gte IE 6)]>
                        <input type="text" name="log" id="log" value="Username" />
                    <![endif]-->
                    <!--[if !(IE)]><!-->
                        <input type="password" placeholder="Password" name="pwd" id="pwd" />
                    <!--<![endif]-->
                    <!--[if (gte IE 6)]>
                        <input type="password" value="Password" name="pwd" id="pwd" />
                    <![endif]-->    
                    <p style="clear:both;width:115px;font-size:14px!IMPORTANT;float:left;">
                        <input style="width:14%!IMPORTANT;" name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me
                    </p>


                    <input type="hidden" name="redirect_to" value="<?php echo $_SERVER[\'REQUEST_URI\']; ?>" />
                    <p style="float:right;">
                        <a style="font-size:14px!IMPORTANT;" href="<?php echo get_option(\'home\'); ?>/wp-login.php?action=lostpassword">Recover password</a>
                    </p>                                
                    <input style="margin-left:80px;float:left;" type="submit" name="submit" value="LOGIN" class="login-button" />                               
                </form>
                <div class="clear"></div>
            <?php } else { ?>
                <p>
                    You are currently logged in, would you like to <a href="<?php echo wp_logout_url($_SERVER[\'REQUEST_URI\']); ?>">logout</a>?
                </p>
            <?php } ?>  
我猜以下是。。。

我需要对用户名(在上面的表单中输入)做些什么,然后将其与任何帖子的作者进行匹配,如果匹配,则将其发送到该页面。。。在提交上述表格之前。这需要在隐藏字段之前完成,以便将用户名字段输入到隐藏的输入值中。

4 个回复
SO网友:Jake

如果我是你,我会使用wp\\u login\\u form函数创建你的表单,但看起来你一切都对了,也许你有理由使用手动表单。

<?php wp_login_form(); ?>
无论哪种方式,都可以使用此操作更新登录重定向,将其添加到函数中。php:

add_action( \'login_redirect\', \'custom_redirect_login\', 10, 3 );

function custom_redirect_login( $redirect_to, $request, $user )
{
    $posts = get_pages( array(
        \'authors\' => $user->ID
    ) );
    if (! empty($posts) )
    {
        // Since the pages are listed in DESC order, the first one is the most
        // recently created.
        return get_permalink($posts[0]->ID);
    }
    else
    {
        // If no posts associated with the user, use default.
        return $redirect_to;
    }
}
如前所述,如果需要重定向到post或custom\\u post\\u类型而不是页面,则需要使用非页面特定的方法,因此这可能会更好,因为它可以更普遍地工作:

function custom_redirect_login( $redirect_to, $request, $user )
{
    $posts = new WP_Query( array( \'author\' => $user->ID ) );
    if ($posts->have_posts())
    {
        // Since the pages are listed in DESC order, the first one is the most
        // recently created.
        return get_permalink($posts->posts[0]->ID);
    }
    else
    {
        // If no posts associated with the user, use default.
        return $redirect_to;
    }
}

SO网友:Warwick

创建并存储信息的项目是“页面”还是“帖子”。

如果是“post”,则前一个答案中的过滤器中的get\\u页面将不起作用(它将仅获取post类型为“页面”的项目)。试用此筛选器。

function custom_redirect_login( $redirect_to, $request, $user )
{
    $posts = new WP_Query( \'author=\'.$user->ID );
    if ($posts->have_posts())
    {
        // Since the pages are listed in DESC order, the first one is the most
        // recently created.

        return get_permalink($posts->posts[0]->ID);
    }
    else
    {
        // If no posts associated with the user, use default.
        return $redirect_to;
    }
}

SO网友:Rob

这目前可以使用,但感觉有点脏!如果有更好的方法,我宁愿这样做。

<script>
function onSubmit(){
    var str = document.forms["login"]["log"].value;
    var str = str.replace(/\\s+/g, \'-\').toLowerCase();
    document.forms["login"]["redirect_to"].value = str;
}
</script>                   

                    <?php if (!(current_user_can(\'level_0\'))){ ?>   
                        <form action="<?php echo get_option(\'home\'); ?>/wp-login.php" method="post" name="login" onsubmit="onSubmit()">
                            <p style="color:black!IMPORTANT;">Please login.</p>
                            <!--[if !(IE)]><!-->
                                <input type="text" placeholder="Username" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" />
                            <!--<![endif]-->
                            <!--[if (gte IE 6)]>
                                <input type="text" name="log" id="log" value="Username" />
                            <![endif]-->
                            <!--[if !(IE)]><!-->
                                <input type="password" placeholder="Password" name="pwd" id="pwd" />
                            <!--<![endif]-->
                            <!--[if (gte IE 6)]>
                                <input type="password" value="Password" name="pwd" id="pwd" />
                            <![endif]-->    
                            <p style="clear:both;width:115px;font-size:14px!IMPORTANT;float:left;">
                                <input style="width:14%!IMPORTANT;" name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me
                            </p>


                            <input type="hidden" name="redirect_to" value="" />
                            <p style="float:right;">
                                <a style="font-size:14px!IMPORTANT;" href="<?php echo get_option(\'home\'); ?>/wp-login.php?action=lostpassword">Recover password</a>
                            </p>                                
                            <input style="margin-left:80px;float:left;" type="submit" name="submit" value="LOGIN" class="login-button" />                               
                        </form>
                        <div class="clear"></div>
                    <?php } else { ?>
                        <p>
                            You are currently logged in, would you like to <a href="<?php echo wp_logout_url($_SERVER[\'REQUEST_URI\']); ?>">logout</a>?
                        </p>
                    <?php } ?>

SO网友:tobe

也许你可以在我的问题中找到答案:How to get user-meta from Social Login registered users?

我使用Gravityforms让用户只从前端创建一个页面(使用page.php作为模板),并在提交后将他们引导到新创建的页面。(所有内置的Gravityforms设置,创建页面的功能除外,但您可以在我的链接中找到它)

但是,当用户回来时,我希望他们显示一条欢迎消息,其中包含指向他们页面的链接,而不是表单(因此他们只填写一次表单)

对于管理员通过admin dashboard注册的用户来说,它工作得很好。但它不适用于通过社交登录插件注册的用户。(这就是我的问题所在)

因此,我链接背后的部分问题可能是您的答案,但如果您希望用户通过社交登录进行注册,您也会想找到我问题的答案。;-)

我希望这有帮助。

结束

相关推荐

Wordpress login form script

I am looking for something very simple but I\'m not quite sure how to achieve this. I have been researching for the past 2-3 hours and gathered a lot of useful information. However, my knowledge in both PHP and WordPress isn\'t enough to do this from scra