Simple Membership Plugin › Forums › Simple Membership Plugin › Two bugs with the swpm_auto_redirect_non_members() hook provided.
Tagged: swpm_auto_redirect_non_members
- This topic has 4 replies, 3 voices, and was last updated 6 years, 2 months ago by
BoppinCol.
-
AuthorPosts
-
February 5, 2020 at 10:10 pm #19740
BoppinCol
ParticipantI am getting this (seemingly common) GutenbergEditor error.
Everything seems to work, but WordPress fails to update when you try in backend.
Updating failed. Error message: The response is not a valid JSON response.
The error difficult to track down as it isn’t debugged or logged anywhere and the most common advice is uninstall plugins and themes.
I tracked the error down to these lines of code. From this link. https://simple-membership-plugin.com/auto-redirect-non-logged-in-users-protect-whole-site/
add_filter( 'pre_get_posts', 'swpm_auto_redirect_non_members' ); function swpm_auto_redirect_non_members() { if (is_admin()){ //Inside the admin dashboard. Nothing to do. return; } //We are on the front-end. Lets check visitor's login status. if( !SwpmMemberUtils::is_member_logged_in() && !is_page( array( '', 'login' )) ) { wp_redirect( 'http://www.your-domain.com/membership-login' ); exit; } }I managed to solve the problem in the backend by reverting to the code you provided previously.
/*** Auto redirect a user who isn't logged into the site ***/ add_filter( 'pre_get_posts', 'swpm_auto_redirect_non_members' ); function swpm_auto_redirect_non_members() { if (is_admin()){ // I added this block after seeing swpm use it. I don't think was here originally // Inside the admin dashboard. Nothing to do. return false; } if( !SwpmMemberUtils::is_member_logged_in() && !is_page( array( '', 'login' ) ) && !current_user_can('administrator') ) { wp_redirect( 'https://www.your-domain.com/' ); exit; } }So fortunately everything seems to be working again.
I am intrigued into what actually causes this error, perhaps you may know? I can reproduce it on my site. Perhaps you won’t be able to.
But I tried uninstalling all the other plugins to fix this problem before finding the solution. I am really curious to know about this particular error as it’s so closely related with the Gutenberg Editor and hard to track/debug. I may mention it on WP Stackoverflow. If it’s okay with you ?
I have also noticed that these lines of code for a redirect none logged in users will also cause a PHP warning / notice on the front-page.
[05-Feb-2020 21:13:02 UTC] PHP Notice: Trying to get property 'ID' of non-object in /home/sites/3a/5/5af1210914/public_html/wp-includes/class-wp-query.php on line 3965https://core.trac.wordpress.org/ticket/21790
https://core.trac.wordpress.org/ticket/27015I found the solution below at the above tickets. Would it not be possible to place this filter in a different wordpress function??
/*** Auto redirect a user who isn't logged into the site ***/ add_filter( 'pre_get_posts', 'swpm_auto_redirect_non_members' ); function swpm_auto_redirect_non_members($query) { $front_page_id = get_option( 'page_on_front' ); $current_page_id = $query->get( 'page_id' ); $is_static_front_page = 'page' == get_option( 'show_on_front' ); if ( $is_static_front_page && $front_page_id == $current_page_id ) { error_log( 'is static front page and current page is front page' ); return; } if (is_admin()){ // Inside the admin dashboard. Nothing to do. return; } if( !SwpmMemberUtils::is_member_logged_in() && !is_page( array( '', 'login' ) ) && !current_user_can('administrator') ) { wp_redirect( 'https://www.your-domain.com/' ); exit; } }February 5, 2020 at 10:18 pm #19742BoppinCol
ParticipantThe first issue is really weird.
Basically if I don’t include
&& !current_user_can('administrator')as a condition then that breaks the Gutenberg on my theme. Which is a fairly simple child of 2016. My WP install is up-to-date and i’m using php7.3
It’s particularly interesting because it looks to me, a non-php person that line of code should never be invoked on the backend.
February 7, 2020 at 4:57 am #19750mbrsolution
ModeratorThank you for reporting this issue to us. I have submitted a message to the developers to investigate further your findings.
Kind regards
February 7, 2020 at 5:22 am #19751admin
KeymasterIt is hard to troubleshoot custom coding related issues without being in that environment and changing lines of codes on the fly.
I have a feeling that adding a check against the wp_doing_ajax() function call will be good here.
You can try adding the following before that code is executed:
if ( function_exists('wp_doing_ajax') && wp_doing_ajax()){ //wp_doing_ajax() so no need to execute this code return true; }Alternatively, maybe execute that code on “init” hook of WordPress (instead of “pre_get_posts” filter hook).
February 7, 2020 at 2:33 pm #19753BoppinCol
ParticipantThanks for both of your replies.
I have quickly reproduced this error in local env with, https://localbyflywheel.com/community/t/local-lightning-public-beta/14075
On my live site, which currently is just my personal private development environment, with no users, my solution above works well. No errors.
I’m tried calling swpm_auto_redirect_non_members() on init and it just crashed everything!!!
I don’t think (but don’t know 100%) that the WP main loop has been invoked. I think the is_page() function requires it to be within the loop. Sorry I am not better at WP and PHP!
I tried adding your new code block. Not sure where you meant to place it… But the original
Updating failed. Error message: The response is not a valid JSON response.persists.
/*** Auto redirect a user who isn't logged into the site ***/ add_filter( 'pre_get_posts', 'swpm_auto_redirect_non_members' ); function swpm_auto_redirect_non_members($query) { if ( function_exists('wp_doing_ajax') && wp_doing_ajax()){ //wp_doing_ajax() so no need to execute this code return true; } $front_page_id = get_option( 'page_on_front' ); $current_page_id = $query->get( 'page_id' ); $is_static_front_page = 'page' == get_option( 'show_on_front' ); if ( $is_static_front_page && $front_page_id == $current_page_id ) { error_log( 'is static front page and current page is front page' ); return; } if (is_admin()){ // Inside the admin dashboard. Nothing to do. return; } if( !SwpmMemberUtils::is_member_logged_in() && !is_page( array( '', 'login', 'hello-world' ) ) ){ wp_redirect( 'http://swpmtest.local/' ); exit; } }In other words it still needs,
&& !current_user_can('administrator')At the end of that conditional before the redirect. Which is super weird as that line should never be invoked. Think you’re on to something regarding ajax though as I don’t need to refresh the backend if I add or remove that conditional. Maybe that’s to expected behaviour but I’m just trying to be helpful!
I have mentioned it here,
I hope that you’re not annoyed by that. I can delete it if so. I’m really puzzled by this problem. Even though I have ‘fixed’ the issue I really want to know why it was breaking.
I’m sure you could change the arg in current_user_can(‘some-string’) to something different and still get the desired effect.
I really enjoy using this plugin so I wanted to point these things out to you guys in case you get others with a similar issue.
-
AuthorPosts
- You must be logged in to reply to this topic.