The fundamental problem is that the code tries to use the REST API endpoint before the endpoints have been registered.
Specifically it needs to do this after the rest_api_init
hook has fired. Try to do it on a hook that runs after plugins and theme are loaded such as wp_loaded
.
In general avoid doing work in the root scope of a plugins file or functions.php
. WordPress is architected around hooks and filters and a particular load process, so it’s more accurate to think of hooks as events on a timeline. By just putting stuff in the plugin file without using a hook, it’s essentially running in prehistoric times. You can’t ask it to drive from A to B because cars haven’t been invented yet.
So this isn’t a problem of what, it’s a problem of when
Related Posts:
- How to check WordPress website username and password is correct
- WordPress REST API call generates nonce twice on every call
- How to: Rest endpoint returning empty object
- Custom endpoint that returns an xml
- Plugin Sidebar is not Saving Meta Attribute to Post/Page after “Update”
- Rest Api How to get results of child categories with one api call
- WP rest api v2 posts filter by ‘search’ & ‘category_name’ not working
- What method to use to call a REST API?
- WP REST API V2 – Add user data to response
- How can I pass get_the_author_meta(‘user_email’) through the REST API?
- How to get results of subcategories also?(WP Rest Api v2)
- Amending REST API function without deactivate/activate plugin every time changes is made
- Create a new post using rest api and save featured image using an external image url
- Adding custom end points, No error line
- Serve text/html from wp-json API via WPEngine, headers not being set properly
- Secure REST with nonce works only when not logged
- How to do POST request with WP REST API v2 to custom table?
- rest_no_route on custom API endpoint wordpress
- Custom Rest Endpoint: Post/Put/Patch not working
- Can’t retrieve body params from PUT endpoint using formdata
- Do I need to create an endpoint?
- How to use apiFetch to get author information in Gutenberg properly?
- Mock a dependency of a WP_REST_Controller class
- update_callback is not working in register_rest_field
- REST Endpoint API 404
- Allow REST API Endpoint to specific user and hide from public
- Custom Endpoint – Does it possible to use PUT method with WP API Rest?
- How to do admin ajax request in a plugin for rest api
- Attaching Image-file to userId
- Query String Filtering API
- Custom plugin & calling REST api of wordpress instance the plugins is running on
- GET request return value as error instead of success
- How to query a nested field in wordpress api using _fields param
- Get Custom taxonomy parent with wordpress REST API
- register_meta not showing custom post type metabox data in rest api
- why is apiFetch throwing Unhandled Promise Rejection: TypeError: Object is not a function
- Securing custom rest API endpoints with public access from PWA
- WP_Query not ordering correctly
- 403 error rest_’cookie_invalid_nonce’ on API Request
- Adding Amchart Interface to WordPress API
- WC REST API Error Handling
- Initializing rest endpoint issue
- Multisite and the JSON REST API: How to?
- React in plugins: REST endpoints vs. regular functions
- Query only title/field/featured media of posts [closed]
- WP API users per page
- Best place to keep files that are called by cron jobs?
- Is it safe to use PUT and DELETE requests
- Is it possible to make WordPress as a RESTful app?
- oneOf two possible objects in WP REST API?
- How to update WordPress Plugins in your own maintance application?
- Extend Plugin that creates an endpoint for GET request to also accepts POST requests
- How can create a custom plugin to call my webapi after any registration or membership plugin functionality
- Rest Api WordPress
- External api call using wordpress
- Does WP REST API cache internally executed (rest_do_request) requests?
- Run different permission_callback depending on HTTP method of custom REST API endpoint
- REST API error in block editor for custom templates
- How to get locale within WP REST Request?
- Execute callback after REST API request completes + Response is sent
- How to boost WP custom post REST API GET queries by custom taxonomies
- What should I use instead of WP_CONTENT_DIR and WP_PLUGIN_DIR?
- Getting a peer review for my new plugin?
- PHPUnit testing WordPress Plugin
- Why does WordPress create two transients with the same name when I specify timeout value?
- Determine which theme location a wp_get_nav_menu_items is for
- How to use wp_set_password in a plugin?
- URL rewriting must be activated manually for my plugin
- how to activate plugin network-wide, and save setting across sites
- PHP logging framework to be used with WordPress
- What is difference between blog id and site id?
- How to Load Plugin JS in theme’s footer section
- wp_transition_post_status does not change the status of the post
- Add notification bubble notice in navigation using transients
- Custom plugin – $post_id in wp_head
- WooCommerce registration password field not displaying
- wordpress plugin php file processing form
- How To Change Post Author Default?
- is it recommended to use WP_List_Table?
- Uploaded attachment not set as featured image
- WordPress – manually relate a post to page(s)?
- I have this error notice ‘wp_enqueue_script was called incorrectly’ in my plugin
- wpdb->get_var always returning 0
- A function that will remove HTML and tags from a string?
- Questions about meta box: nonce and parameters available
- Remove Products From Category
- Using WordPress with WP Statistics for QR code usage survey
- Add default value from selection
- How to Login a User inside a Plugin and Redirect to page?
- Gutenberg component in a plugin admin
- Add a new tag based on the category name in the publish event
- WordPress function is not called and ajax return 0
- Caption Shortcode: what filter to change the image size?
- How to search on the field that stores escaped strings?
- Issue on Getting WP Gallery Items In cpt-single.php As Attachments
- Adding Meta box to thrid-party plugin
- Identifying a Page Containing Shortcode at `init`
- How to conditionally include a custom field on category editor screen not category “add” screen
- Method not receiving attributes from shortcode call, general OOP problems
- wp_head is not fired from the hook I have used in my plugin