This reference covers the REST endpoint, the Abilities API ability, and the filters and actions behind Ask the Docs in Knowledge Base Pro. For setup and settings, see Ask the Docs: AI answers from your knowledge base.

Ask the Docs needs WordPress 7.0 or later. On older versions none of these hooks, routes or abilities are registered.

REST endpoint

POST /wp-json/wzkb/v1/ask with a JSON body:

{ "question": "How do I get a refund?" }

Questions must be 3 to 300 characters after HTML tags are stripped and whitespace is collapsed.

Response

FieldTypeDescription
answeredboolWhether the knowledge base answered the question.
answerstringThe answer as plain text. Empty when not answered.
sourcesarrayArticles the answer came from, each with id, title and url. Empty when Show sources is off.
sources_titlestringHeading for sources, translated for the number of articles.
relatedarrayUp to three other published articles from the same section, or with the same tags, as the main source. Each has id, title and url. Empty when not answered.
related_titlestringHeading for related, translated for the number of articles.
cachedboolWhether the answer came from the cache.
messagestringThe fallback message when not answered.
search_urlstringThe knowledge base search results URL for the question.
reasonstringdaily_cap, provider_error, provider_paused, provider_busy or invalid_response when there is no answer for one of those reasons. Empty otherwise. provider_paused means every provider is paused after repeated errors.

Errors

StatusCodeWhen
400wzkb_ai_invalid_questionThe question is too short or too long.
403wzkb_ai_forbidden_originThe Origin (or Referer) header doesn’t match the site or an allowed origin.
403wzkb_ai_forbidden_clientThe request has no user agent, or one that looks like a bot, script or HTTP library.
404wzkb_ai_unavailableAsk the Docs is disabled or no provider is available.
429wzkb_ai_rate_limitedThe visitor reached Questions per visitor per hour. The Retry-After header gives the seconds until the next hour.

When the daily cap is reached, the endpoint returns 200 with answered set to false and reason set to daily_cap.

When several visitors ask the same uncached question at once, only the first request calls the provider. The others wait for its answer and return it from the cache. If the provider is still working when the wait ends, they return 200 with answered set to false and reason set to provider_busy, rather than paying for a second provider call.

Answer validation

The provider replies with whether it answered, the answer text and the IDs of the articles it used. Source IDs that were not sent are discarded, and an answer with no valid sources is treated as unanswered. Source links are built from your articles, never from the model’s output, and the answer is returned as plain text. A malformed reply returns reason invalid_response and is not cached, so the next request gets a fresh attempt.

Bot check

Before any cache or provider work, the endpoint refuses requests with an empty user agent, or one that contains any of these, ignoring case: bot, crawl, spider, slurp, scrap, headless, phantom, puppeteer, playwright, selenium, curl, wget, python, httpclient, http-client, okhttp, axios, node-fetch, java/, libwww, guzzle, postman, insomnia.

A script can send a browser’s user agent, so the visitor limit and daily cap remain the limits on cost. Use wzkb_ai_is_bot to allow or block specific user agents:

add_filter(
    'wzkb_ai_is_bot',
    function ( $is_bot, $user_agent ) {
        // Allow an internal monitoring tool.
        return str_contains( $user_agent, 'AcmeMonitor/' ) ? false : $is_bot;
    },
    10,
    2
);

Ability

Knowledge Base Pro registers the knowledgebase/ask ability with the WordPress Abilities API, in the shared webberzone category. It takes { "question": "..." }, returns the same data as the REST endpoint, and runs through the same cache and limits. It is registered only on WordPress 7.0 or later, where Ask the Docs loads.

It is limited to logged-in users by default, because the Abilities API route cannot check that a request comes from your site the way the REST endpoint does. The bot check does not apply to it. Use wzkb_ai_ability_permission to change who can run it:

add_filter( 'wzkb_ai_ability_permission', '__return_true' );

Filters

FilterArgumentsDescription
wzkb_ai_ability_permissionbool $allowed, mixed $inputWhether the current user may run the ability. Default: logged in.
wzkb_ai_allowed_originsstring[] $originsExtra origins allowed to call the REST endpoint.
wzkb_ai_answerarray $result, string $question, array $articlesThe validated answer, before it is cached.
wzkb_ai_answer_cache_ttlint $ttlHow long answers are cached, in seconds. Default one week. Return 0 to turn off caching.
wzkb_ai_canonical_questionstring $canonical, string $questionThe canonical form of a question. Questions with the same canonical form share a cached answer.
wzkb_ai_configured_providersarray $optionsThe providers considered configured, which the Fallback provider setting lists.
wzkb_ai_ask_boxstring $html, array $argsThe HTML of a knowledge base search box that answers questions. $args['context'] is search_form.
wzkb_ai_content_gaps_capabilitystring $capabilityCapability needed for the Content gaps report. Default manage_options.
wzkb_ai_is_botbool $is_bot, string $user_agent, WP_REST_Request $requestWhether a REST request comes from a bot.
wzkb_ai_log_questionbool $log, string $question, array $resultReturn false to stop logging a question.
wzkb_ai_lock_waitint $secondsHow long a request waits for a concurrent request to answer the same question. Default the provider request budget.
wzkb_ai_modelstring $model, string $providerThe model used for a provider. Empty for the provider default.
wzkb_ai_model_optionsarray $options, string $providerThe models offered for a provider in the Model and Fallback model settings.
wzkb_ai_pre_promptnull $response, string $prompt, string $system_instruction, string $question, array $articles, string $providerReturn a string to use as the model’s raw output, or a WP_Error to simulate a provider failure, and skip the provider request. Useful for testing.
wzkb_ai_prompt_builder$builder, string $question, array $articles, string $providerThe WordPress AI Client prompt builder before it is sent, once for each provider tried.
wzkb_ai_provider_cooldownint $cooldown, string $provider, int $status, int $failures, WP_Error $errorHow long a provider is paused after a qualifying error, in seconds.
wzkb_ai_provider_optionsarray $optionsThe providers offered in the AI provider and Fallback provider settings.
wzkb_ai_request_timeoutint $timeoutSeconds each provider is given to answer before the next is tried. Default 20. The answer lock and the browser’s timeout follow from it.
wzkb_ai_responsearray $response, string $questionThe final response sent to the browser or ability.
wzkb_ai_retrieved_articlesarray $articles, string $question, int $limitThe articles sent as context.
wzkb_ai_retrieverRetriever_Interface $retrieverReplace the retriever, for example with an embeddings-based search.
wzkb_ai_retriever_query_argsarray $args, string $questionThe WP_Query arguments used to find articles.
wzkb_ai_skipped_blocksstring[] $blocksBlocks left out of the article text, such as navigation and listing blocks.
wzkb_ai_stop_wordsstring[] $wordsWords dropped from questions before searching.
wzkb_ai_system_instructionstring $instruction, string $answer_lengthThe instructions sent with each request.
wzkb_ai_visitor_ipstring $ipThe IP address used for rate limiting. Return the client IP from a trusted header if your site is behind a proxy or CDN.

Custom retriever

A retriever implements WebberZoneKnowledge_BaseProAIRetriever_Interface. Its retrieve( string $question, int $limit ): array method returns articles, most relevant first, each with id (int), title, url and content (plain text).

add_filter(
    'wzkb_ai_retriever',
    function ( $retriever ) {
        return new My_Embeddings_Retriever();
    }
);

Visitors behind a proxy

add_filter(
    'wzkb_ai_visitor_ip',
    function ( $ip ) {
        // Only trust this header if your proxy always sets it.
        return isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) : $ip;
    }
);

Actions

ActionArgumentsDescription
wzkb_ai_question_askedstring $question, array $result, bool $cachedFires after each answered or unanswered question.
wzkb_cache_clearedNoneFires after Knowledge Base → Tools → Clear Cache clears the output cache. Knowledge Base uses it to invalidate REST API responses, and Knowledge Base Pro to delete cached Ask the Docs answers.
add_action(
    'wzkb_cache_cleared',
    function () {
        delete_transient( 'my_kb_cache' );
    }
);

Caching

  • Answers: transients prefixed wzkb_ai_answer_, kept for a week. The key includes the canonical question (Ask_Handler::canonical_question()), the content version, Answer length, Articles sent as context, Maximum characters per article, the provider and model, and the locale.
  • Article fingerprints: each answer stores a fingerprint of every article sent to the provider, taken before the request: its modified date plus the _wzkb_ai_rev post meta, which changes when the article is updated or its terms change. A changed fingerprint, or an article that is no longer published, makes the answer a miss. Answer_Cache::touch_post() retires the answers built from an article when it changes in a way the plugin cannot see.
  • Content version: the wzkb_ai_content_version option. It changes when the settings are saved, when a knowledge base term is edited or deleted, and when the cache is cleared from the Tools page, and retires every answer.
  • Unanswered version: the wzkb_ai_publish_version option. It changes when an article is published or updated and retires only cached unanswered results.
  • Provider pauses: wzkb_ai_trip_* and wzkb_ai_failures_* transients. A network, rate-limit, quota or server error pauses a provider for 5 minutes, doubling up to an hour, and the next provider in the chain (the AI provider, then the fallback provider) is tried. A successful request clears the pause.
  • Provider status: the wzkb_ai_provider_available transient, kept for a week when a provider is available and for 10 minutes when it isn’t. It is deleted when a provider request fails, when any plugin is activated or deactivated, when the AI provider setting changes, and when the cache is cleared. The AI settings tab checks again in the background when it is missing.

See also

Was this article helpful?