Essentially, you shouldn’t wrap your REST route declarations inside is_admin() since WordPress will not load them (see @DarkNueron comment).
What you can do is pass a ‘permission_callback’ function to the register_rest_route function. If the function returns true, the request is allowed to continue; if false, an error is returned.
So you could do:
register_rest_route('your-namespace/v1', '/options/', [
'methods' => 'PATCH',
'callback' => [__CLASS__, 'update_option'],
'permission_callback' => function () {
return current_user_can('manage_options');
}
]);
Related Posts:
- How do I Enqueue styles/scripts on Certain /wp-admin Pages?
- Changing Admin Menu Labels
- Is there ANY way to remove comments function and section totally?
- Add “external” link to admin menu in the backend
- How to add custom submenu links in wp-admin menus?
- How to allow users login to WP from external domain and make REST requests
- Edit specific nodes in WP_Admin_Bar
- How to add notification bubble for my custom admin menu page
- How to create sub menu with a URL parameter?
- Adding a menu item in the admin bar
- How to remove items from +New admin menu?
- Reorder custom submenu item
- Make sub menu items a main link in the admin menu using fuctions.php
- Add column to pages table
- Admin account only shows Profile and Dashboard with no activity
- How to wrap admin menus and menu headers in additional divs without modifying the WordPress core?
- How to disable admin flyout menus?
- remove plugin admin menu in wordpress without installing plugin in v3.8.1
- How to prevent parent admin page from appearring as a child admin page
- Rename W3 Total Cache admin menu
- Remove query var on admin pages when ‘Save changes’ pressed
- Create a admin page in wordpress without admin menus (“wordpress sidebars”)
- Remove All in One Pack from the admin bar
- Cannot find an OLD Gravity Form on an OLD Word Press site [closed]
- Add column to pages table
- Add menu option to “New Post” menu in admin bar
- Admin menu in front-end
- Remove or move admin submenus under a new menu
- How to get specific page screen ID
- Removing Dashboard Menu Items Through The Database
- WordPress Side Menu Admin Panel Default Order numbers List?
- load-{$page hook} and returned parameter
- Hide admin menu on update_option
- meta box on new admin page
- How to hide Admin Tabs? [duplicate]
- Making Comments, a sub_menu in admin menu
- Admin top level menu, pointing to an external url
- How to break line / add to ADMIN menu
- User Capabilities are not available in WP REST permission callback?
- Creating custom admin panel pages without making a plugin?
- Change users.php WP_User_Query
- How can I control which plugins run in the dashboard?
- how to stop wordpress admin menu from scrolling with page
- Trying to create a page as a menu item in the admin while keeping the admen panel visible
- Custom admin menu order fails if slugs are complicated
- WordPress Admin Panel Left Sidebar No showing on Post create page
- Weird white space in admin area
- Redirect from wp-admin back to page
- How to move “Collapse Menu” to top of admin bar?
- Appearance -> Menus doesn’t show
- restrict admin panel sections to users
- Remove menu item dashboard for a unique user
- How to display status messages in admin panel
- adding existing menu page on new customer user role
- Can I get programmatic access to wp-admin?
- Disable flyout (popup) menus in backend
- Remove Theme menu link from Admin Panel
- Remove capability to interact with a page
- Redirection to external site on admin
- Move Custom Taxonomy Menu to Top Level Admin Menu
- get_current_screen() does not return parent_file
- How can I have an admin only non-dashboard page in my WordPress plugin
- How to display site title with post title?
- Cant login to wp-admin
- How to add attributes to WordPress Admin Sub Menu List Items
- WP-admin or any other is redirecting me to (This page doesn’t seem to exist.)
- Remove WP Admin Menu Items by User Role
- Is There A Plugin to Create WP Multisite Installs programatically
- Adding a button to send email to a user in WordPress Admin Users table
- Our Team not showing on website [closed]
- Get current session in WP admin
- Strange problem with HIdden Input field and WPAlchemy
- Uncaught SyntaxError: Unexpected Token Illegal
- How to remove the Login Customizer menu from the WordPress dashboard
- How to add a field on the user creation page?
- Change the default Post/Page edit title in Admin
- Login redirect_to url is pointing to a url with the port
- data-vocabulary.org schema
- Unable to access my page or WP Admin
- Get current URL in Form action
- My WP-Admin is showing a blank screen
- wp-admin ERR_CONNECTION_TIMED_OUT only on certain networks
- Show comments in Backend
- How to hide comments from private posts on user dashboard
- Conditions for admin get_current_screen action parent_file edit.php?post_type=page
- Replace whole paragraph on multiple pages
- Error establishing a database connection – after updating DNS
- Messed something up, and now I can’t get into wp-admin
- Handling multiple account logins?
- Issue with search form admin panel after PHP migration
- Hook into backbone to add js to wp-admin -> media library?
- Renaming child-theme folder fixed wp-admin blank
- Unable to access dashboard (wp-admin) only
- is_admin() conditional statement
- Admin Login page not working at all
- How to make the Panel // WP-Admin lighter via Mobile
- React Material UI and WordPress Admin Area
- Add Woocommerce Customers link to custom admin menu
- WordPress Add/Edit Post right admin sidebar change title
- How to access admin menu labels and links on front-end?