• I am using a simple script to require users to be logged in to to use my site.

    
    /* FORCE LOGIN ACROSS ENTIRE SITE
    ================================================== */
    add_action('parse_query',function(){
    	if(
    		!is_user_logged_in()
    		&& !in_array($_SERVER['PHP_SELF'],array(
    			'/wp-login.php',
    			'/wp-register.php',
    		))
    	):
    		auth_redirect();
    	endif;
    });
    

    However, the newest React version of Jetpack no longer loads the admin pages correctly. It tells me “Uncaught SyntaxError: Unexpected token < in JSON at position 0”. This is because it is forcing the jetpack calls to go to the login page as well. How could I account for this in my code? Or what in jetpack is calling my site and thinks it isn’t logged in, even though I am obviously logged in to see the management page? Or is this just another bug that will be fixed here in a patch soon?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    Jetpack uses the WordPress REST API to handle the different options presented on the Jetpack dashboard page. It would be worth changing where your function is hooked, and hook it later, once the REST API has been loaded as well. I haven’t tested it, but you could try using the template_redirect hook, for example.

    Let me know how it goes.

    Thread Starter PStevenson

    (@silverokami42)

    That did not work. I modified my code, which is even more hacky now which I do not like, but at least it works. Basically it’s the calls to the plugin file that are breaking it, not sure if there is a better method to write this:

    
    /* FORCE LOGIN ACROSS ENTIRE SITE
    ================================================== */
    add_action('parse_query',function(){
    	$siteURL = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    	$siteURL = parse_url(str_replace(get_bloginfo('url'), '', $siteURL));
    	if(
    		substr($siteURL['path'],0,27)!='/wp-content/plugins/jetpack'
    		&&!is_user_logged_in()
    		&& !in_array($_SERVER['PHP_SELF'],array(
    			'/wp-login.php',
    			'/wp-register.php',
    		))
    		&& !is_page_template( 'page-templates/VT_signup.php' )
    	):
    		auth_redirect();
    	endif;
    });
    
    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    What happens if you hook your original function even later? You could try shutdown, since it’s the last action available.

    Let me know how it goes.

    Thread Starter PStevenson

    (@silverokami42)

    That did not work either. All of those still force the files referenced in /wp-content/plugin/jetpack to the login page still. However, I did come up with this cleaner work-around, that with the correct url references now, should be able to copy/paste into any theme:

    /* FORCE LOGIN ACROSS ENTIRE SITE
    ================================================== */
    add_action('parse_query',function(){
    	$currentURL = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    	if(
    		( !defined(JETPACK__PLUGIN_DIR) || strpos($currentURL, plugin_dir_url(JETPACK__PLUGIN_DIR)) !== false )
    		&&!is_user_logged_in()
    		&& !in_array($_SERVER['PHP_SELF'],array(
    			'/wp-login.php',
    			'/wp-register.php',
    		))
    	):
    		auth_redirect();
    	endif;
    });

    Since the files in the plugin directory are the ones causing the issue, I can just check if the current URL is in that plugin’s directory.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    That’s interesting. It might be worth updating to Jetpack 4.3.1, and then revert to the original version of your code, just to make sure the problem isn’t just caused by some of the issues we fixed in Jetpack 4.3.1.

    If that doesn’t help, could you open the browser’s dev console, expand the JavaScript error, and make a screenshot so I can take a closer look?

    Thanks!

    • This reply was modified 9 years, 4 months ago by Jeremy Herve.
    Thread Starter PStevenson

    (@silverokami42)

    That does seem to fix it by upgrading it. I will keep an eye on it and let you know if anything changes in future patches. Thanks for your follow up on this.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘logged out / JSON Error’ is closed to new replies.