search

Home  >  Q&A  >  body text

WordPress function is_user_logged_in is not working properly in siteurl and wp_redirect.

<p>I tried modifying site_URL and wp_redirect using the following code: </p> <pre class="brush:php;toolbar:false;"><?php add_filter( 'site_url', 'my_prefix_site_url', 10, 2 ); add_filter( 'wp_redirect', 'my_prefix_wp_redirect' ); function my_prefix_site_url( $url, $scheme ) { return my_prefix_modify_url( $url, $scheme ); } function my_prefix_wp_redirect( $url ) { return my_prefix_modify_url( $url, null ); } function my_prefix_modify_url( $url, $scheme = null ) { $current_url = isset( $_SERVER['PHP_SELF'] ) ? sanitize_text_field( wp_unslush( $_SERVER['PHP_SELF'] ) ) : ''; if ( ! strpos( $current_url, 'wp-admin' ) && ! is_user_logged_in() ) { return '/'; } return $url; } </pre> <p>But I encountered the following error.
P粉358281574P粉358281574593 days ago330

reply all(1)I'll reply

  • P粉253518620

    P粉2535186202023-07-29 18:11:47

    I don't really understand what the actual purpose of this code is, if I understand it correctly I think it breaks a few things, but to your literal question: just write your own function i.e. Can.

    The is_user_logged_in function is not complex in the default version:

    function is_user_logged_in() {
        $user = wp_get_current_user();
    
        return $user->exists();
    }
    

    And wp_get_current_user is also pluggable but simple:

    function wp_get_current_user() {
        return _wp_get_current_user();
    }
    

    So you'd just write this:

    function custom_is_user_logged_in() {
        return _wp_get_current_user()->exists();
    } 

    reply
    0
  • Cancelreply