Viewing 5 replies - 1 through 5 (of 5 total)
  • Same with me… Rest API doesn’t work if Restricted Site Access is activated.

    Any help?

    Same here, I can’t access the API even with the right credentials.

    So like me, if anyone using this plugin just to only allow authenticated users, and want access to the rest api, you can use this script I have just created.

    Add it to your functions.php in the theme root.

    add_action('init','srs_restrict_public_access');
    function srs_restrict_public_access(){
        global $wp;
        if ( !in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) && !empty( $wp->query_vars['rest_route'] )){
            if(!is_user_logged_in()){
                $current_path = empty( $_SERVER['REQUEST_URI'] ) ? home_url() : $_SERVER['REQUEST_URI'];
                $redirect_url = wp_login_url( $current_path );
                wp_redirect( $redirect_url, '302' );
            }
        }
    }

    @sandyrig

    This did not work for me….

    Parse error: syntax error, unexpected ‘$wp’ (T_VARIABLE) in

    @sandyrig’s solution doesn’t work for me either.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘I can't use Rest API’ is closed to new replies.