Looks like I found a snippet that do exactly that.
It’s from Daniel Bachhuber, the API developer.
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;
});
This is posted in his gist on GitHub.
Related Posts:
- With Rest V2 (WP4.7) how does one restrict certain RESTFUL verbs?
- Fetch All Posts (Including Those Using a Custom Post Type) With WordPress API
- How to assign specific users the capability to edit specific pages / posts / custom post types
- Show custom post type endpoint in REST API just if user has capability
- Allow Users Access to Custom Post Type Only
- Custom post type capabilities require “create_posts” to access the edit posts list page
- How do I get the intended post type of a revision post?
- How to restrict CPT post’s fronted view only for specific user roles?
- Exclude objects from WordPress API based from ACF field using rest_prepare_{$post_type}
- Custom REST endpoint not working to retrieve single posts (“rest_no_route”)
- How to handle this specific case of custom post type?
- How can I remove “Add new” button on custom post type
- Capabilities and Custom Post Types
- Custom post type role permissions won’t let me read
- Allow user to Edit Posts but not Add New?
- How do I Filter Custom Post Type by Custom Taxonomy in the newest WordPress RESTful API?
- Confusion with adding meta capabilities to a role after registering a Custom Post Type with corresponding ‘capability_type’ parameter
- Notice: map_meta_cap was called incorrectly
- WP Rest API Querying Custom Posts by ACF fields
- How to not allow custom roles to edit published custom post types?
- How to get dynamically custom post type that are under a certain category
- Get the Category Name instead of ID from WP-API
- create a back-end wordpress content submission using custom post type
- How do I code access to the built-in UI of a CPT when it’s placed as submenu of another CPT that is protected by role?
- Get custom post type REST API not working
- Custom role capabilities to administrator not taking effect (no plugin)
- Prevent author role from editing all posts in custom post type?
- How to add custom fields to admin UI and REST API response?
- Custom search with Custom Fields in WP REST API?
- Custom Post Type and API REST is not working
- React post to WordPress custom post type
- How would you associate location data with a post?
- Custom Post-to-Post with multiple parents?
- Cannot attach media when capabilities added to custom post type
- Add custom parameter to REST API request of a custom post type?
- Custom Taxonomy Invalid in REST API
- Role capability delete multiple post type posts doesn’t work
- Define new user capability for custom post types?
- Cannot add custom field to “orderby” parameter in Rest API
- Getting meta in editor plugin, and event triggering issue
- Adding user capabilities for a custom taxonomy of a custom post type for a custom user
- Restrict Access to Posts based on Custom User and Post Meta Data
- Database to page routing API?
- Expose a custom field of a custom post type to the REST API
- Custom post type archive page – Posts overview – with one modal for multiple posts?
- Callback to custom field is not working in WordPress REST API
- delete_published_posts does not work
- Custom Post Type not showing in Rest API on Multisite
- Able to edit custom post, but unable to create new custom post when within submenu. What capability is missing?
- Allow add new post access to custom post but not wp post for some role
- Cannot Create new Post within Custom Post Type
- Create custom post with custom user rules
- How to add/edit advanced custom fields on custom post type’s WordPress REST API?
- Create custom post with meta field with AJAX and the WordPress REST API
- WordPress API for custom post types returns rest_no_route
- Conflict between Capabilities and Menu Visibility with Custom Post Types
- Adding guestbook to my wordpress site
- Role capabilities issue
- WP API Response does not show my registered metadata
- REST filters only firing when I have a param set
- How to handle new post from API request?
- wp-json API: not logged in when clicking link to the API from admin mode
- WordPress as a backend for external service? [closed]
- Restrict category access to specific users/groups. Author always has access
- Custom capabilities not reflecting on admin panel
- Custom Post Type not working with Capabilities
- Post/Custom Post Type URL Access
- Roles and Capabilities in Custom Post Types
- custom post type capabilities – needs to be VERY secure
- How to fix the Post Preview Button (CPT & map_meta_cap)
- Restrict Custom Post Type per role in Dashboard
- Restrict access to custom post type based on its taxonomy terms
- How can I create new CustomPostType record using wp.api.collections?
- Custom endpoint filtering post by custom taxonomies
- wp rest api orderby field in a custom table
- How To Read Read Custom Post Type Data in Headless CMS Mode
- URL issue retrieving Custom Post Types using Backbone JS API
- How to add field to custom post type endpoint
- How can I add a meta[] to my custom post type and search by term with the Rest API?
- Can’t see post status drop down for custom post type
- Issue with CPT posts within WP REST API showing as []
- Saving custom post types post_meta over REST-API fails
- WooThemes – Vendors / Bookings – Allow Vendors to manage resources
- How do you make a custom post type invisible for subscribers
- WordPress API only returning HTML
- How to create new permission for custom post types for doing specific tasks
- Capabilities and mapping required for a role to be able to edit other’s posts of a custom type, BUT only be able to edit their own blog posts
- Weird capabilities / roles behavior
- creating different edit screens for different roles
- Capability to edit own post, but not create new one
- What would prevent a custom role from viewing posts (both standard and custom)?
- Capability to read user’s own draft post of CPT
- Custom post type & role issues
- Allowing logged in users to create custom posts
- wp_insert_post not working for custom post type?
- Restrict editing of a custom post type based on status and user capabilities
- Store custom post type with JSON content
- Create settings page to enable or disable CPT
- Get all posts of any post type in a category from REST API
- How to consume external API from WordPress post editor and display the response data in the custom field?