This plugin registers secure API endpoints that allow you to request translations directly from DeepL without exposing your Deepl API-key. These endpoints are only accessible when a valid nonce is provided. When providing translations to website visitors, you can configure which languages are supported for translation.
Each object that is translated will store its cached translation in the wp_postmeta table within the database. This caching mechanism ensures that translations are efficiently reused, reducing unnecessary API requests to DeepL and saving costs.
post_modified date of the object, the cached version is served.post_modified date of the object is more recent than the cached translation, a new translation is fetched from DeepL. Once retrieved, this translation is immediately cached for future use.edit_posts capability (or a custom capability configured via the yard::deepl/cache_capability filter) are permitted to create new cache entries. Requests from users without this capability will still return a translation from DeepL, but the result will not be stored in the cache.This approach minimizes the number of API calls to DeepL, ensuring translations are kept up to date only when necessary.
A metabox labeled Yard DeepL is displayed on the edit screen of supported post types (default: page). It provides two options:
A Translation cache column is added to the post list screen of all supported post types. Its purpose is to notify editors which posts should be cached as soon as possible to avoid unnecessary DeepL API costs.
The uncached call count is only incremented for visitor requests (users without cache-write capability) and is automatically reset per language once a cache entry is stored. Posts with the Disable translation cache option enabled are excluded from the column entirely.
This plugin connects to the DeepL API to provide translations for content.
The API endpoints registered by this plugin are secured using a WordPress nonce. The nonce is passed to the front-end using the wp_localize_script function and is stored in a global JavaScript object ydpl which contains the following properties:
ydpl_translate_post_id: The ID of the post to be translated.ydpl_rest_translate_url: The URL of the API endpoint for translation requests.ydpl_supported_languages: The list of languages supported for translation.ydpl_api_request_nonce: The nonce used for API validation.When making requests to the API, ensure that the nonce is included in the request headers. The header should be named X-WP-Nonce, and it should contain a nonce created with the wp_rest action (available via ydpl.ydpl_api_request_nonce on the front-end).
Rate limiting: Unauthenticated requests (or requests from users without the cache capability) are limited to 3 requests per 60 seconds per IP address. Authenticated users with the required cache capability (default: edit_posts) are exempt from this rate limit.
Cache capability: Only users with the edit_posts capability can trigger cache creation. This can be customized using the yard::deepl/cache_capability filter.
Request:
var xhr = new XMLHttpRequest();
xhr.open('POST', ydpl.ydpl_rest_translate_url, true);
// Set request headers
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('X-WP-Nonce', ydpl.ydpl_api_request_nonce);
// Handle response
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log('Translation:', JSON.parse(xhr.responseText));
} else if (xhr.readyState === 4) {
console.error('Error:', xhr.statusText);
}
};
// Prepare and send the request body
var data = JSON.stringify({
text: ["Look another test"],
target_lang: "DE"
});
xhr.send(data);
Response:
[
{
"text": "Look another test!",
"translation": "Sehen Sie sich einen weiteren Test an!"
}
]<h3>Filters</h3>
* yard::deepl/cache_capability (default: edit_posts) — The WordPress capability required to create cache entries. Users without this capability receive translations but results are not cached.
* yard::deepl/cache_metabox_post_types (default: ['page']) — Post types on which the Yard DeepL cache metabox is displayed.
* yard::deepl/disable_cache_metabox_post_types — Deprecated. Use yard::deepl/cache_metabox_post_types instead.