WordPress functionality to redirect a specific page to the login page and then back to that page
P粉794851975
P粉794851975 2023-08-30 16:27:52
0
1
487
<p>I searched online and on stackoverflow but didn't find what I wanted, this is my situation</p> <p>I want to redirect users who want to access a specific page to the login URL and then redirect them to the previous page I used the following code</p> <pre class="brush:php;toolbar:false;"><?phpif ( is_user_logged_in() || ! is_page() ) return; $restricted = array( 5049 ); // All restricted pages if ( in_array( get_queried_object_id(), $restricted ) ) { $previous_url = $_SERVER['HTTP_REFERER'] ? : site_url( '/user-account' ); wp_redirect( $previous_url); exit(); }});</pre> <p>The first part worked fine, I was redirected to the login page, but then not redirected to the old page</p> <p>I would like to know how to redirect to the page the user wants to visit after successful login</p>
P粉794851975
P粉794851975

reply all(1)
P粉006540600

Create your own login page using redirect parameters:

<?php 
   //获取当前页面ID
   $id = get_queried_object_id();

   $args = array(
       'echo'           => false,
       'redirect'       => get_page_link($id), 
       'form_id'        => 'loginform',
       'label_username' => __( '用户名' ),
       'label_password' => __( '密码' ),
       'label_remember' => __( '记住我' ),
       'label_log_in'   => __( '登录' ),
       'id_username'    => 'user_login',
       'id_password'    => 'user_pass',
       'id_remember'    => 'rememberme',
       'id_submit'      => 'wp-submit',
       'remember'       => true,
       'value_username' => NULL,
       'value_remember' => true
   ); 
        
   // 调用登录表单。
   $form = wp_login_form( $args );

   //显示表单
   echo $form;
?>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template