Understanding the wp-json Endpoint in WordPress
The wp-json URL is the entry point to the WordPress REST API. When a request reaches your-site.com/wp-json, WordPress returns a JSON document that describes the available routes and namespaces. Developers use this interface to create headless front‑ends, mobile apps, or custom integrations that read and write content without loading the full admin interface.
Because the endpoint is public by default, site owners sometimes need to verify that it is working, restrict access, or extend its capabilities. The following sections explain how to confirm the endpoint’s status, enable it if it appears missing, and adjust its behavior for specific use cases.
Checking Whether wp-json Is Accessible
Before making changes, confirm the current state. Open a browser and navigate to:
https://example.com/wp-json
If you see a JSON object that begins with {"namespaces":[ and lists core namespaces such as wp/v2, the endpoint is reachable. A blank page, a 404 error, or an HTML error message indicates that something is blocking the request.
You can also use the command line:
curl -I https://example.com/wp-json
A successful response returns HTTP 200 and a Content-Type: application/json header.
Common Reasons the Endpoint Appears Disabled
WordPress core does not provide a switch to turn the REST API on or off. Instead, the endpoint becomes inaccessible due to:
- Permalink settings set to the plain structure (
?p=123). The rewrite rules that map/wp-json/toindex.php?rest_route=are not generated. - Security plugins that block REST API requests for unauthenticated users.
- Server configuration (e.g., Nginx) that denies access to
wp-jsonvia location rules. - Custom code in
functions.phpor a mu‑plugin that removes the REST API filters.
Identifying the cause guides the appropriate fix.
Enabling wp-json Through Permalinks
The simplest method is to ensure WordPress uses a pretty permalink structure. Follow these steps:
- Log in to the WordPress dashboard.
- Navigate to Settings → Permalinks.
- Select any structure other than “Plain” (for example, “Post name”).
- Click Save Changes. WordPress flushes the rewrite rules and creates the necessary redirect for
/wp-json/.
After saving, revisit the wp-json URL. You should now see the JSON response. If the problem persists, move on to the next checks.
Adjusting Plugin‑Based Restrictions
Many security suites (e.g., Wordfence, iThemes Security, Sucuri) include an option to disable the REST API for visitors who are not logged in. Locate the REST API section within the plugin’s settings:
- Wordfence: Wordfence → All Options → Brute Force Protection and look for “REST API” toggles.
- iThemes Security: Settings → WordPress Tweaks → REST API.
- Sucuri: Settings → Hardening → REST API.
If the option is enabled to “Disable REST API for non‑logged‑in users,” uncheck it or adjust the rule to allow the specific namespaces you need (e.g., wp/v2). Save the changes and test the endpoint again.
Using Code to Allow or Block Access
When a plugin does not expose a setting, you can add a small snippet to your theme’s functions.phpfile or a custom plugin.
Allowing unrestricted access (for development)
add_filter( 'rest_authentication_errors', function( $result ) {
if ( ! empty( $result ) ) {
return $result; // another plugin already returned an error
}
// No authentication required
return false;
} );
Returning false tells WordPress the request is valid without credentials. Use this only on staging sites; exposing the API publicly can lead to abuse.
Requiring authentication for all endpoints
add_filter( 'rest_authentication_errors', function( $result ) {
if ( ! empty( $result ) ) {
return $result;
}
if ( ! is_user_logged_in() ) {
return new WP_Error( 'rest_not_logged_in', 'You must be logged in to access the REST API.', array( 'status' => 401 ) );
}
return false;
} );
This snippet forces a login before any REST call succeeds. Place it in a mu‑plugin (/wp-content/mu-plugins/rest-auth.php) to ensure it runs before other plugins.
Extending wp-json with Custom Routes
Developers often need to add their own endpoints. The process involves registering a route with the rest_api_init hook.
Example: a simple greeting endpoint
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/greeting/(?P<name>\w+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'myplugin_greeting_callback',
'args' => array(
'name' => array(
'validate_callback' => function( $param, $request, $key ) {
return preg_match( '/^[a-zA-Z]+$/', $param );
},
),
),
);
} );
function myplugin_greeting_callback( $data ) {
$name = sanitize_text_field( $data['name'] );
return rest_ensure_response( array( 'message' => 'Hello, ' . esc_html( $name ) . '!' );
}
After adding the code, visit https://example.com/wp-json/myplugin/v1/greeting/John to receive a JSON greeting. This pattern scales to more complex resources such as custom post types, settings, or external data.
Security Considerations When Exposing wp-json
While the REST API facilitates powerful integrations, it also expands the attack surface. Follow these practices to keep the endpoint safe:
- Limit exposure to trusted clients. Use IP‑based restrictions in your web server or a firewall plugin if only certain servers should consume the API.
- Enforce authentication for actions that modify data (POST, PUT, DELETE). Cookie‑based authentication works for logged‑in users; for external apps consider application passwords or JWT tokens.
- Regularly audit installed plugins. A poorly coded plugin may inadvertently expose sensitive data via an unintended namespace.
- Monitor logs for unusual spikes in
/wp-json/requests. Tools like WP‑Activity Log or server‑level access logs help detect abuse attempts. - Keep WordPress core, themes, and plugins updated. Security patches often address REST‑API‑related vulnerabilities.
Troubleshooting Common Errors
404 Not Found
If the URL returns a 404, verify permalinks are not set to Plain. Re‑save the permalink page to flush rewrite rules. On Nginx, ensure a location block like the following exists:
location / {
try_files $uri $uri/ /index.php?$args;
}
401 Unauthorized
A 401 response means the endpoint demands authentication. Check whether a security plugin or code snippet enforces login. If you intend to allow public read access, adjust the corresponding setting or remove the restrictive filter.
Empty Response or HTML Error Page
Some caching plugins mistakenly cache the REST API output as HTML, leading to a blank JSON body. Exclude /wp-json/ from caching rules. In WP Rocket, add the path to “Never Cache URLs”. In WP Super Cache, add it to the “Rejected URI Strings” list.
Slow Response Times
Large sites with many registered routes can experience latency. Use a caching layer specifically for REST responses (e.g., the WP REST Cache plugin) and monitor the number of active namespaces. Unused plugins that register unnecessary routes should be deactivated.
Best Practices for Day‑to‑Day Management
Consider the following checklist when maintaining the wp-json endpoint:
- Confirm the endpoint returns JSON after any major WordPress update.
- Review security plugin settings after updates; they sometimes reset REST API toggles.
- Document any custom routes you add, including required authentication methods and expected input formats.
- Use versioning in your custom namespaces (
myplugin/v1) to allow future changes without breaking existing integrations. - Test API calls with tools like Postman or Insomnia before deploying them in production.
- Regularly rotate application passwords if you use them for external services.
Frequently Asked Questions (FAQs)
Do I need to install a separate plugin to enable the REST API?
No. The REST API is part of WordPress core. Enabling it usually involves adjusting permalink settings or removing any blocks imposed by security plugins or custom code.
Can I disable the REST API for logged‑in users only?
Yes. Using the rest_authentication_errors filter you can allow anonymous read access while requiring login for write operations, or vice versa, depending on your site’s needs.
Will disabling the REST API affect the Gutenberg editor?
Gutenberg relies on the REST API for saving blocks and retrieving post data. Turning off the API will break the editor’s functionality, so keep it enabled for the admin area.
How do I know which namespaces are currently registered?
Request https://example.com/wp-json and look at the namespaces array in the returned JSON. Each entry corresponds to a plugin or core feature that has added routes.
Is it safe to expose the wp-json endpoint to the internet?
It is safe when proper authentication and permission checks are in place. Public read‑only access to content (posts, pages) is generally acceptable, but any endpoint that modifies data should require authenticated requests.
What is the difference between cookie authentication and application passwords?
Cookie authentication uses the logged‑in user’s session cookie and works naturally within the browser. Application passwords are generated per user and can be sent in the Authorization header, making them suitable for mobile apps or external services that cannot maintain cookies.
Can I cache REST API responses?
Yes. Plugins such as WP REST Cache or server‑level solutions like Varnish can store JSON responses for a defined TTL, reducing load on PHP and the database.
Summary
The wp-json endpoint is a core feature that enables programmatic interaction with WordPress. Ensuring it is accessible involves checking permalink settings, reviewing security‑plugin restrictions, and, if necessary, adding or removing code filters. Once reachable, you can secure the endpoint with appropriate authentication, extend it with custom routes, and monitor its performance and safety. By following the practices outlined above, you can make the REST API work reliably for headless projects, mobile apps, or any integration that requires structured data from WordPress.