docs-v2 icon indicating copy to clipboard operation
docs-v2 copied to clipboard

Document how to require authentication on all API requests

Open kadamwhite opened this issue 8 years ago • 2 comments

This is a common issue (WP-API/WP-API#2432 WP-API/WP-API#2497), and the snippet here https://gist.github.com/danielbachhuber/8f92af4c6a8db784771c (copied below) should be provided within the documentation site itself.


<?php
add_filter( 'rest_authentication_errors', function( $result ) {
    if ( ! empty( $result ) ) {
        return $result;
    }
    if ( ! is_user_logged_in() ) {
        return new WP_Error( 'restx_logged_out', 'Sorry, you must be logged in to make a request.', array( 'status' => 401 ) );
    }
    return $result;
});

kadamwhite avatar May 13 '16 21:05 kadamwhite