Your AI agent has a WordPress application password. The username is right, the site is up, and every write attempt comes back 401 or 403. The response does not tell you which of the six problems below you actually have, so people start rotating passwords that were never broken.
I connect agents to WordPress sites most weeks, and these two codes cover almost every failure I see. WordPress is unusually honest about this once you know where to look: the status code tells you which half of the stack failed, and the error body names the exact check that rejected you. Two curl commands usually finish the diagnosis.
This is the order I run through, most common cause first. Every step shows the output you should expect, so you can tell within a minute whether you are looking at a hosting problem, a capability problem, or a request that never carried credentials at all.

401 and 403 Mean Different Things (Read the Body First)
WordPress picks between these two codes with one line of core code: rest_authorization_required_code() returns 401 when is_user_logged_in() is false, and 403 when it is true.
That is the whole distinction. A 401 means WordPress never identified your agent as a user. A 403 means it knew exactly who the user was and then rejected the action. If you only remember one thing from this post, remember that.
The error body narrows it further. These are the codes I see most:
rest_not_logged_in(401): credentials missing, malformed, or never reached PHP.rest_cannot_create(403): the user lacksedit_postsfor that post type.rest_cannot_publish(403): the user can create drafts but lackspublish_posts.rest_cannot_edit(403): editing a post owned by someone else withoutedit_others_posts.rest_no_route(404): wrong path, or the post type hasshow_in_restdisabled.
One more check before anything else: is the response JSON at all? If you get an HTML block page instead of a JSON error object, WordPress never ran. That HTML came from Cloudflare, a WAF, or the host. Look for server: cloudflare and cf-ray in the response headers. This one distinction has saved me more time than any other.
Cause 1: The Authorization Header Never Reaches PHP
This is the most common cause by a wide margin, and the one that makes people doubt their own sanity. Basic auth travels in the Authorization request header. When PHP runs as CGI or FastCGI under Apache, which is how most cPanel and many managed stacks run it, Apache does not forward that header to PHP unless you explicitly tell it to. $_SERVER['HTTP_AUTHORIZATION'] is simply absent. WordPress sees an anonymous request and returns 401 even though the password is perfect.
It usually works on your laptop and fails on the live site because local tools such as Local and Valet wire PHP up differently. Same credentials, different SAPI, different result.
Here is the test that isolates it. The pretty permalink goes through the .htaccess rewrite rules, while ?rest_route= reaches the same controller without them:
Both 401 means the credentials are the problem. Pretty URL 401 but rest_route 200 means the header is being dropped in the rewrite or proxy layer. Nothing is wrong with the password, and no amount of regenerating it will help.
On Apache, add this to .htaccess above the WordPress block:
# Apache: pass the Authorization header through to PHP
RewriteEngine On
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
On Apache 2.4.13 or newer, CGIPassAuth On in the directory block does the same job in one line. On nginx, add fastcgi_param HTTP_AUTHORIZATION $http_authorization; to the PHP location block. Then restart PHP-FPM. If that is not something you can edit on your host, this is a support ticket, not a plugin problem, and it is worth naming the exact header in the ticket.

Cause 2: The User Authenticates Fine But Cannot Publish
Authentication and authorization are separate steps. Every REST route runs a permission_callback, and those callbacks check capabilities, not passwords. A Contributor account is the classic trap: it has edit_posts, so draft creation works, but it does not have publish_posts. The agent runs happily for a week, then someone sets "status": "publish" in the payload and every request starts returning:
{"code":"rest_cannot_publish","message":"Sorry, you are not allowed to publish posts as this user.","data":{"status":403}}
Confirm what the account can actually do before you touch the payload:
wp cap list editor | grep -E "publish_posts|edit_others_posts|assign_terms" publish_posts edit_others_posts assign_terms
If those three lines are missing for your agent’s role, that is your bug. The fix is a role with the capabilities it needs and nothing more. Editor works for most publishing agents. If you want a narrower role, create one:
add_role( 'agent_publisher', 'Agent Publisher', array(
'read' => true,
'edit_posts' => true,
'publish_posts' => true,
'edit_others_posts' => true,
'upload_files' => true,
'assign_terms' => true,
) );
Do not skip assign_terms. Without it, sending categories in the payload fails with rest_cannot_assign_term even though the post itself would have been allowed. This is the error that looks like a taxonomy bug and is really a role bug.
One more edge case: a plugin can remove capabilities through the map_meta_cap or user_has_cap filters. If WP-CLI says the role has publish_posts and REST still says no, deactivate plugins one at a time on staging and retest. Something is filtering the check after the role is read.
Cause 3: Application Passwords Are Switched Off, or the Proxy Hides HTTPS
Application passwords have a hard requirement: wp_is_application_passwords_available() returns false when is_ssl() is false. No SSL, no app password auth, and you get a 401 with no useful explanation.
The non-obvious version of this happens behind Cloudflare or a load balancer. TLS terminates at the proxy, PHP sees plain HTTP, is_ssl() is false, and core refuses the login even though the browser address bar shows a padlock. If your host terminates SSL upstream, add this to wp-config.php above the require_once ABSPATH . 'wp-settings.php' line:
if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
$_SERVER['HTTPS'] = 'on';
}
Two plugins also turn app passwords off by design. Wordfence has a disable toggle under Login Security, and Solid Security has its own. If the Application Passwords section is missing from the user profile screen, a plugin or the host already disabled it. Do not work around that with an admin password in a config file. Fix the toggle.
Cause 4: The Request Is Not Sending Basic Auth at All
Most agent frameworks have both an API key field and an auth field. Pasting an application password into a Bearer token field produces a request WordPress ignores, because core only understands Basic auth for app passwords. The 401 looks identical to a wrong password.
Look at what actually goes on the wire:
curl -v -u "$WP_USER:$WP_APP_PASSWORD" https://example.com/wp-json/wp/v2/users/me 2>&1 | grep -i authorization > authorization: Basic YWdlbnQtZWRpdG9yOmpiY2Qg...
If that line is absent, the client is not authenticating. If it is present and you still get 401, go back to Cause 1. Application passwords are 24 alphanumeric characters shown in groups of four, and core strips every non-alphanumeric character before hashing, so spaces, dashes, and a stray newline in an environment variable are all harmless. The password format is almost never the problem. The header is.
Verify the Fix With a Real Round Trip
Do not stop at /users/me. That endpoint only proves authentication. Run the exact write your agent will run, then clean up after yourself:
curl -s -u "$WP_USER:$WP_APP_PASSWORD" -X POST https://example.com/wp-json/wp/v2/posts \
-H "Content-Type: application/json" \
-d '{"title":"Agent connectivity test","status":"draft"}' | jq '{id,status,link}'
{
"id": 4021,
"status": "draft",
"link": "https://example.com/?p=4021"
}
A 201 with an ID means auth, capabilities, and the post type all work. Delete the test post with DELETE /wp-json/wp/v2/posts/4021?force=true so it does not sit in your drafts forever. If the draft works but publishing fails, you are back at Cause 2 and the missing capability is publish_posts.
Common Mistakes
- Testing with a browser session and assuming it proves anything. A logged-in browser sends cookies, not Basic auth. It exercises a completely different code path.
- Giving the agent an Administrator account to make the errors stop. It works, and it also gives a prompt-injected agent the ability to install plugins, create users, and edit theme files. Scope the role instead.
- Hardcoding
"status": "publish"from day one. Draft first, review, then publish. It turns a bad agent run into a rejected draft instead of a live incident. This is the pattern I use in my AI agent guardrails walkthrough. - Rotating the password before checking the header. If
?rest_route=works and the pretty URL does not, the credential was never the problem. - Treating a 403 the same as a 401. One is a capability bug, the other is a delivery bug. Fixing the wrong one wastes an afternoon.
If your hosting is the bottleneck: Kinsta keeps your site safe with automatic daily backups and one-click restore, so a bad update or a hack is a rollback, not a rebuild. Their support is top tier, and 24/7 live chat is staffed by WordPress engineers who will help fix the actual problem with you, so you are not stuck waiting on your developer to reply. Plans start at $35 per month.
Not sure Kinsta fits? Compare the top WordPress hosting options first
Read our guide to connecting an AI agent to WordPress with application passwords, including the scoped user account and the exact REST calls.
Read the Setup GuideFrequently Asked Questions
Postman usually sends Basic auth correctly, and your agent may be putting the password in a Bearer token field instead. Check the outgoing request for an authorization header that starts with Basic. If it is there and you still get 401, your host is probably dropping the header before PHP sees it.
A 403 means WordPress identified the user and then a capability check failed. The usual missing capability is publish_posts, which Contributors do not have. Read the error code in the response, since rest_cannot_publish and rest_cannot_create point at different fixes.
Yes. WordPress checks is_ssl() before it will accept an application password. If TLS terminates at Cloudflare or a load balancer and PHP does not see HTTPS, authentication fails even though the browser shows a padlock. Setting HTTPS from the X-Forwarded-Proto header fixes it.
No. Administrator gives the agent the ability to install plugins, create users, and edit theme files, which is a large blast radius for a prompt injection. An Editor account, or a custom role with publish_posts and assign_terms only, covers almost every publishing workflow.
Go to Users, open the profile, and revoke the application password you issued to it. From WP-CLI, run wp user application-password list followed by delete. Changing the account role takes effect immediately and is the fastest way to stop writes during an incident.
Final Thoughts
The next time an agent gets a 401 or 403 from WordPress, read the body before you touch the credentials. rest_not_logged_in is a delivery problem, rest_cannot_publish is a capability problem, and an HTML response is not a WordPress problem at all. Three categories, three different fixes.
Then make the fix permanent: one scoped user, one application password, draft first, and a short log line every time the agent writes. My WordPress security checklist covers the rest of the account hardening if you want to close the loop.

