Skip to content

Instantly share code, notes, and snippets.

@t0b1hh
Last active September 21, 2023 12:36
Show Gist options
  • Save t0b1hh/2cdd24b97c98828c4d83221a37c7a29e to your computer and use it in GitHub Desktop.
Save t0b1hh/2cdd24b97c98828c4d83221a37c7a29e to your computer and use it in GitHub Desktop.
WPML API returning error 403 because of missing user agent

If the registration API of the wordpress plugin WPML / OTGS returns an error 403 or "invalid response" try to add an user-agent header to the api call.

wp_remote_post() seems to send w/o user-agent - perhaps depending on your installation/server-config.

Add this php snippet to your functions.php or some custom plugin:

function my_http_request_args($args) {
  $args['user-agent'] = 'WordPress';
  return $args;
}
add_filter('http_request_args', 'my_http_request_args');

You may change the string 'WordPress' into whatever. "Something" worked as fine as "curl/7.74.0".

Perhaps you could also use the http_header_useragent filter instead of http_request_args. Haven't tested that.

192 'user-agent'          => apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ), $url ),

https://developer.wordpress.org/reference/classes/wp_http/request/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment