search

Home  >  Q&A  >  body text

WordPress functionality to redirect a specific page to the login page and then back to that page

<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粉794851975P粉794851975573 days ago580

reply all(1)I'll reply

  • P粉006540600

    P粉0065406002023-08-31 00:27:41

    Create your own login page using redirect parameters:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    <?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;

    ?>

    reply
    0
  • Cancelreply