# Feed your own data to the chatbot’s knowledge base

> Code recipes to add custom fields, external data and members-only content to the knowledge base, weight sources, and use WPBot's search in your own code.

Source: https://www.wpbot.pro/help/dev-extend-the-knowledge-base/  
Section: Developers › Extending and templates  
Updated: 2026-11-26

The knowledge base is what the chatbot searches before the AI answers. The owner chooses its sources in **WPBot Pro → AI / Settings → Knowledge Base** (pages, posts, products, custom post types, documents and more). With a few hooks you can feed it data the settings screen cannot reach. Put the code in a small plugin, then click **Sync now** on the Knowledge Base screen so existing content is read again.

Tip

Before you write code, check the screen: custom post types, **Include Post Meta Data** (custom fields), **Custom fields (ACF)**, **Database tables** and **Upload and sync** already cover most needs.

## Add a custom field to a post's text

`qcld_wpbot_core/kb_post_text` changes the text stored for each post. Here the delivery time of each product is added:

```
add_filter( 'qcld_wpbot_core/kb_post_text', function ( $text, $post ) {
	if ( 'product' === $post->post_type ) {
		$days = get_post_meta( $post->ID, 'delivery_days', true );
		if ( $days ) {
			$text .= "\n\nDelivery time: " . absint( $days ) . ' business days.';
		}
	}
	return $text;
}, 10, 2 );
```

When the field changes without the post being saved (for example by an import), sync that post again with `\QuantumCloud\WPBotCore\Knowledge\Kb_Sources::resync_later( $post_id )`, or add the meta key to `qcld_wpbot_core/kb_resync_meta_keys`.

## Add data that is not a post

`qcld_wpbot_core/kb_sources` registers a source with a label, a count and a function that returns items in batches. Each item needs `id`, `title`, `url` and `content` (optional `language`):

```
add_filter( 'qcld_wpbot_core/kb_sources', function ( array $sources ) {
	$sources['branches'] = array(
		'label' => 'Branches',
		'count' => function () {
			return count( my_branches() );
		},
		'items' => function ( $offset, $limit ) {
			$items = array();
			foreach ( array_slice( my_branches(), $offset, $limit ) as $b ) {
				$items[] = array(
					'id'      => (string) $b['id'],
					'title'   => $b['name'],
					'url'     => home_url( '/branches/' . $b['slug'] . '/' ),
					'content' => $b['name'] . ': ' . $b['address'] . '. Open ' . $b['hours'] . '.',
				);
			}
			return $items;
		},
	);
	return $sources;
} );
```

To push single changes as they happen (from a webhook or a cron job of yours), store or remove items directly:

```
qcld_wpbot_core_kb_store( 'branches', array(
	'id'      => '12',
	'title'   => 'Berlin branch',
	'url'     => home_url( '/branches/berlin/' ),
	'content' => 'Berlin branch: Example Street 1. Open Monday to Friday, 9:00 to 18:00.',
) );
qcld_wpbot_core_kb_remove( 'branches', '7' );
```

Vectors for new text are made in the background by WP-Cron and count against the **Knowledge base** spending limit.

## Members-only knowledge

Each item can carry **access labels**. The chatbot only searches items whose labels the visitor holds:

- Everyone holds `public`; logged-in visitors also hold `logged_in` and `role:<role>`.
- WPBot Pro adds `purchased:*` for customers (Professional and Master).
- Your membership or course plugin can add more.

Label the content, then give visitors the matching labels:

```
// Posts in the "members" category are for Gold members only.
add_filter( 'qcld_wpbot_core/kb_post_access', function ( $labels, $post ) {
	if ( has_term( 'members', 'category', $post ) ) {
		$labels = array( 'membership:gold' );
	}
	return $labels;
}, 10, 2 );

// Tell the chatbot which visitors are Gold members.
add_filter( 'qcld_wpbot_chat/visitor_labels', function ( $labels, $message, $user ) {
	if ( $user && my_is_gold_member( $user->ID ) ) {
		$labels[] = 'membership:gold';
	}
	return $labels;
}, 10, 3 );
```

Items without labels are public. Messages from Messenger, WhatsApp and Telegram have no WordPress user and see public items only. The same labels decide whether the chatbot may read the page a visitor is on (`qcld_wpbot_chat/page_context_allowed`).

## Prefer some sources

`qcld_wpbot_core/kb_search_args` adjusts every search. `weights` takes a source type or prefix and a factor from 0 to 5 (0 leaves the source out):

```
add_filter( 'qcld_wpbot_core/kb_search_args', function ( $args, $query ) {
	$args['weights'] = array(
		'post:wpbot_article' => 2,   // Help Center articles count double.
		'post:post'          => 0.5, // Blog posts count half.
	);
	return $args;
}, 10, 2 );
```

Post types appear as `post:<post type>`; your own sources under the id you registered.

## Use WPBot's search in your own code

```
$results = qcld_wpbot_core_kb_search( 'return policy', array( 'top_k' => 3 ) );
foreach ( $results as $r ) {
	printf( '<a href="%s">%s</a>', esc_url( $r['url'] ), esc_html( $r['title'] ) );
}
```

Without `labels`, only public items are searched. Pass the visitor's labels to include what they may see.

## Check the result

Open **AI / Settings → What the chatbot knows** to see what the knowledge base holds, and ask the chatbot a question that only your new data answers. Logged in as an administrator, you see the real error in the chat if something fails.

## Availability

In every edition, including WPBot Free. The settings screen offers fewer built-in sources in Starter and WPBot Free (no DOCX, sitemap, Google Sheets and Docs, database tables or post meta); the hooks work in every edition.
