Machine Translation with WordPress AI

PerfLocale can translate posts, terms, strings and custom fields through the AI Client that WordPress 7.0 and later include. This guide covers what you need, how long posts are handled, what to do on a slow provider or host, what it costs, and what each error means. The Machine Translation page covers every provider and setting.

What you need

  1. WordPress 7.0 or later. Core ships the AI Client, but no AI service.
  2. A provider plugin for your AI service, from wordpress.org, for example AI Provider for OpenAI or AI Provider for Anthropic. Install and activate it like any plugin (network-activate it on a multisite network).
  3. The service’s API key, in one of three places WordPress reads, first match wins:
    • an environment variable named after the provider, such as OPENAI_API_KEY or ANTHROPIC_API_KEY;
    • a constant of the same name in wp-config.php: define( 'OPENAI_API_KEY', '…' );;
    • the key field on Settings → Connectors.
  4. PerfLocale set to use it: on PerfLocale → Settings → Integrations → Machine Translation, choose WordPress AI Client. The settings link to Settings → Connectors. If the separate AI plugin’s Connector Approval is on, approve PerfLocale there (see The separate “AI” plugin).

PerfLocale asks for a small, low-cost model (OpenAI mini, Anthropic Haiku, Google Flash, in that order); the first one a provider with a key offers is used. To pin a model or a provider, use perflocale/mt/wp_ai_client_args. Tools → Site Health and the block editor say when no provider is ready.

Long posts

A model answers a long text slowly, and a long answer can come back cut off, so long content goes in parts. The part size depends on the AI provider WordPress uses, measured on the real models so that 9 of 10 parts answer within 7 seconds: 2,500 characters with OpenAI, 1,000 with Anthropic and with any other provider. PerfLocale reads the provider from your settings and keys, without a request. Parts are cut only between whole blocks, or between the rows, list items or paragraphs of one block, never inside a paragraph or heading; see Empty items and long content. perflocale/mt/piece_chars changes the size.

What happens to a long post depends on where you start it:

  • + Create and the editor’s Translate button send up to 3 parts while you wait (perflocale/mt/interactive_pieces). A post that needs more is handed to a background job: + Create reopens the original with “This post is sent in 10 parts, so it is machine-translated in a background job. The translation appears when the job is done.”, and the REST route answers HTTP 202 with the job id. This also applies to users with the Translator role; the job runs as the user who asked, with that user’s rights checked again when it runs.
  • Bulk translate and Auto-translate on publish always run as background jobs.
  • WP-CLI (wp perflocale translate) translates in the command, without a time limit.

Watch a job on PerfLocale → Jobs; users who cannot manage translations see their own jobs there. When a handed-off job fails, or keeps an existing translation, the user who asked sees why on the original’s edit screen. A job keeps each finished part, so a post continues in the next run where the last one stopped and no part is paid twice.

Slow providers and cron

With WordPress’s own cron (WP-Cron), a background job runs in short web requests: by default each run has about 25 seconds for provider calls. A part is started only when the time the provider recently needed fits in what the run has left. When the provider needs longer than one run (two calls cut at the run’s time limit), the job stops with “The provider’s recent answers took 35 seconds or more, longer than one background run has (25 seconds), so the job was stopped.” and at most two cut calls are paid for. Three ways out:

  1. Run background jobs from the system cron with WP-CLI (best): set define( 'DISABLE_WP_CRON', true ); and run wp cron event run --due-now every minute from your host’s cron. WP-CLI runs have no time limit.
  2. Allow longer web runs, if your host lets a request live that long: raise perflocale/jobs/bulk_translate/slice_seconds (for example to 55). A run never goes past perflocale/jobs/web_slice_max_seconds (60 by default, at most 90), so raise that too for a value above 60.
  3. Send smaller parts with perflocale/mt/piece_chars.

A request from + Create or the editor always ends within 25 seconds, even when perflocale/mt/request_args raises the timeout: a call that would not fit, or does not answer in time, hands the post to a background job (“The provider answers too slowly for this request…”). When the provider asks to wait (HTTP 429, or 503 with Retry-After), a wait of up to 60 seconds that fits is waited for; a longer one pauses a background job until then, hands an editor request to a job, and after 10 waits in a row a job stops and says so.

Costs and what is never sent

  • Each part, and the title and excerpt, is one request, billed by your AI service by tokens. A post’s parts are each billed once; only a call cut at a time limit can be billed twice (at most two per job).
  • Never sent: text without a letter or digit (empty containers, spacers, images without alternative text), <script> and <style> elements, shortcode tags, template tokens such as {FAQ_PLACEHOLDER} or , block markup and settings, and blocks marked “Do not translate”.
  • The monthly character limit counts what is sent and stops before it is passed.

Errors and what to do

An error names the item and its length, never its text, for example “Failed item: content part 3 of 10 (2,431 characters).”, then the cause:

  • “No AI provider is connected.” Install a provider plugin and add its key (What you need).
  • “HTTP 401. The AI provider refused the key or access” or “refused the API key or access, so the job was stopped”. Check the key on Settings → Connectors (or the environment variable or constant), and the AI plugin’s Connector Approval; then retry. Until the key works nothing is queued.
  • “temporarily unreachable (circuit breaker open; retry in 240s)”. The provider failed several times; calls pause for a few minutes, and background jobs wait and continue by themselves.
  • “The provider did not answer within 10 seconds (the request timed out)”. The model was slow; try again, or see Slow providers and cron.
  • “The provider asked to wait 40 seconds”. A rate limit; try again then. A background job continues on its own.
  • “longer than one background run has”. See Slow providers and cron, then retry the job on PerfLocale → Jobs.
  • “content part 3 of 7, block core/paragraph (7,120 characters)”. One block is longer than a part and has no safe cut; shorten it, or raise perflocale/mt/piece_chars.
  • “was not saved because … block markers …”, “… HTML structure …”, “… template tokens …” or “… escaped text …”. The model changed markup it must keep; nothing was saved. Translate again; if it repeats, pin another model with perflocale/mt/wp_ai_client_args.
  • “needs about 70 MB of memory”. A very long post; raise perflocale_mt_memory_limit or use WP-CLI.