Add permission_callback
parameter and set it to whatever authentication you want to apply.
So, if you want to make it accessible to a user having manage_options
capability, your code should be something like this-
add_action('rest_api_init', function(){
register_rest_route('fs/v1', 'posts', [
'methods' => 'GET',
'callback' => 'fs_posts',
'permission_callback' => function( $request ) {
return current_user_can( 'manage_options' );
}
]);
});
If it’s for any logged-in users, for example, use this-
add_action('rest_api_init', function(){
register_rest_route('fs/v1', 'posts', [
'methods' => 'GET',
'callback' => 'fs_posts',
'permission_callback' => function( $request ) {
return is_user_logged_in();
}
]);
});
Related Posts:
- Pass PHP variable to javascript
- Namespaces in WordPress – How do I initiate the main class?
- wordpress get meta value by meta key
- What areas to Unit test while building a plugin?
- update_meta_user difference in i:1 and b:1, how to write b:1
- Registration Form Validation in wordpress
- Errors while using ajax from external wordpress page
- Dequeue / Deregister script and replace it with a new plugin
- How to handle autoloading with composer by keeping the WordPress naming conventions?
- Hide Theme options and Customize Admin menu
- Current user in plugin returns NULL
- How can I implement a notification system in wordpress?
- Minimum PHP Required for WordPress
- Building an email signup form. Where should the information be saved in the DB?
- Where should I save an API key of my user when installing my plugin?
- How to make a customize role and view a specific plugins base on that role?
- Custom form action to handle data inside a plugin
- WP Post Template – Templates in own folder
- Create entire wordpress as a github repositery?
- Map Custom Registration Fields to WordPress User Roles
- custom payment gateway in woocommerce failed to connect to remote api server
- Plugin options page with live preview?
- Ajax call returning 0
- How to update post’s content on post publish?
- Conditional attributes and logic per product category
- WordPress plugin admin html being shown in Customizer iframe
- Displaying Custom Sidebar Without Modifying Theme’s Core File?
- WordPress Widget – Saving multidimensional arrays into $instance
- Ordering taxonomies by rank
- Plugin Options not being output
- Can I create a table on my DB without creating a plugin?
- Submit Form data to another page via Ajax (WordPress Way)
- WordPress checkbox and Illegal string offset
- Is it fair to use MIT licensed code in WordPress plugins? [closed]
- How to add an element right after the article using jQuery?
- How to reset wordpress but not plugins and settings
- add_submenu_page returns null
- get_option / wp_localize_script Not Working in OOP Plug In
- Switch from wp-cron to a server cron job
- How to Show Author image and url in WordPress plugin directory? [closed]
- Extending a theme: build new features as plugin or core modules?
- Plugin generating unexpected output – on WP mysql dump import
- Remember the Meta Value With Radio Buttons
- jQuery code didn’t run on my plugin
- How to embed a new string in url?
- Custom characters of tinymce editor are saved as question mark
- Custom Module, Extending WP with a Plugin?
- How do I profit from a Plugin?
- help intercepting save_post through plugin
- best way to run a php script away from the template?
- Themes conflicts and overrides my WordPress Support widget Plugin styles
- Disable Author Archive just for certain roles in bulk
- Add a new category for posts
- How to save Setting pages data to the database?
- Add hook after content without formatting
- User loggedin as Subscriber form not sending
- wc_get_template_part doesnt display the content [duplicate]
- How to properly use wp_mail() in my own plugin?
- Prevent WordPress from sending set-cookie http header
- admin_post hook not firing function inside class
- Thumbnail image doesn’t show up in Shortcode output
- I created a plugin with ‘view page’ that uses MYSQL + PHP. When I use wp_insert_post(), it turns everything to HTML
- list of custom post by custom field in frontend
- How to implement color picker from WordPress in my boilerplate plugin?
- custom post type plugin error [closed]
- how to auto random increment for post view
- Fatal error: require_once(): Failed opening required
- How to properly enqueue jQuery knob on WordPress without conflict?
- WordPress Thickbox Navigation Issue in Gallery
- Admin menu hilighting wrong item for CPT
- How to save the option’s new values plus old value without overwrite old one
- Not displaying Woocommerce product page in lightbox
- ajax voting for custom post type
- WordPress Media Uploader : How to add custom button on featured image tab
- Change pricing in Woocommerce based on Category and Product [closed]
- Can we validate data from jquery
- WordPress environment not loading properly
- Delete postmeta when uninstall/delete plugin
- how can i get records from wp_postmeta table using particular meta_key in database?
- Re-writing MySQl query for execute faster
- Inactive Plugin Files
- Saving an array to get_options
- Getting error “The package could not be installed. PCLZIP_ERR_BAD_FORMAT ”
- show Visual form builder in the front end
- Plugin – including external javascript calling google maps
- Help me with my first very basic plugin
- wordpress plugin | short code issue
- HTML link within my plugin settings page
- How to create post comparison in wordpress
- How can I create a navigation menu in the sidebar that can also act like a slider?
- WordPress Customise
- How to create a sharing marketplace with wordpress for bicycles?
- Cannot edit Sidebar in any theme in wordpress
- how to update wordpress plugin programmatically using plugin path
- Testing Plugin – Install plugin dependencies in testing enviroment
- Form submission issue in wordpress admin (custom plugin) using $_GET for searching & filtering
- Editing a block to add a button to have a drop cap
- Redirrect after facebook successful login to wooommerce cart page url
- Remove custom menu pages for admin users
- Show posts in custom order