WordPress function is_user_logged_in is not working properly in siteurl and wp_redirect.
P粉358281574
P粉358281574 2023-07-29 00:18:33
0
1
328
<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粉358281574
P粉358281574

reply all(1)
P粉253518620

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();
} 
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template