Function Calling Overview
Function Calling (also called Tools) needs some functions to be callable so they can be executed. Note that the AI itself does not run code—it can’t execute functions directly. Instead, it knows that it has access to a list of predefined functions that you’ve created.
When the AI model determines that it wants to execute a function, it sends back a reply with the name of the function and any necessary arguments. Then, your server (in the case of AI Engine) runs the function, generates an output, and sends it back to the AI model so it can finalize its response.
Dynamic and Static
There are two types of functions that can be called: Dynamic and Static.
A dynamic function is used to enrich the AI’s final reply with additional data. Dynamic functions can also be called in a cascade, depending on the functions you’ve defined and the AI’s decisions. For example, if you ask the AI to “send me an email,” it might first call a function to retrieve your email information, then call another function to actually send the email.
It’s recommended to use dynamic functions, as the final answer will be written by the AI and will therefore flow naturally. You can register your functions using Code Engine or manually.
Code Engine
Code Engine was previously called Snippet Vault, which is now deprecated. So make sure you’re using Code Engine to continue receiving updates.
The first step is to download the Code Engine plugin. This plugin is 100% free and can be used similarly to Code Snippet, but with even more features. If you’re familiar with Code Snippet, you can even import all your code from there for an easy switch. You can find the plugin here: Code Engine.
Code Engine allows you to store PHP and/or JS snippet code inside a dashboard and execute them whenever you want, even using a REST API.
You have a special type of snippet in Code Engine called Callable. As the name suggests, this snippet can only contain one unique PHP function. You can declare its arguments and define them in specialized side settings. Once this is done, the AI model can use them easily, and you don’t have to do anything more on your side.
By using Code Engine, you can declare Callable functions that can be used in Function Calling. Code Engine and AI Engine will then work together automatically to handle the entire pipeline—asking to run a function → running the function → returning the result to the AI → continuing the query—without you needing to do anything. This is very powerful and is the only plugin on WordPress that can do this!

Simply navigate to your chatbot settings and locate the new "Functions" section. Then, select the function you want to use for the current chatbot session. From there, function calling is automatically handled, streamlining the process for you.
The Function section is not appearing? This means the model you have selected is not compatible with function calling. Only OpenAI and Claude currently support this feature, but not all of their models can use function calling. So make sure you select a model that supports it.


Here, we’re running a PHP callable function—so when the AI wants to run the function, it will be executed on your server. This is really useful if you need to access the database, get user information, fetch posts, and so on.
But it’s not limited to that. You can also create a JavaScript callable function to interact with your website. For instance, you could ask Nyao to change the color of the website—and watch it happen live!
Manual Function Calling
Part 1: Declaring Functions
There are two ways to declare functions for use with AI Engine:
Method 1: Register Functions Naturally
This filter allows functions to be selectable in the Chatbot’s settings, just like Snippet Vault registered functions.
add_filter( 'mwai_functions_list', function ( $functions ) {
$functions[] = define_userInfo();
$functions[] = define_sendEmail();
return $functions;
}, 10, 1 );
To use this filter, you will need to have your function defined. Let’s have a look at how to define functions.
You can define your functions using Meow_MWAI_Query_Function::fromJson()
:
return Meow_MWAI_Query_Function::fromJson( [
'id' => 'userInfo',
'type' => 'manual',
'name' => 'getCurrentUserInfo',
'desc' => 'Get the current user information.',
'args' => [
[
'name' => 'license',
'type' => 'string',
'required' => true,
'desc' => 'User serial key.',
],
],
] );
Method 2: Force-add Functions to the Query
This method adds functions directly to the query, but won't work with OpenAI Assistants, since they need to be added to the Assistants before use.
add_filter( 'mwai_ai_query', function ( $query ) {
$query->add_function( define_userInfo() );
$query->add_function( define_sendEmail() );
return $query;
}, 10, 1 );
Part 2: Handling Function Calls
Once functions are declared, you need to implement how they're called. There are two approaches, depending on how you declared you functions. If you declared them with Method 1 you need handle them with Method 1 as well.
Method 1: Provide Feedback to the AI Model
This method allows the AI model to process the function's output and incorporate it into the final answer.
add_filter( 'mwai_ai_feedback', function ( $value, $needFeedback ) {
$function = $needFeedback['function'];
if ( $function->id === 'userInfo' ) {
return call_userInfo();
}
else if ( $function->id === 'sendEmail' ) {
$subject = $needFeedback['arguments']['subject'];
$message = $needFeedback['arguments']['message'];
return call_sendEmail( $subject, $message );
}
return $value;
}, 10, 2 );
Method 2: Override the AI Reply
This method allows you to completely override the AI's reply based on the function's output.
add_filter( 'mwai_ai_reply', function ( $reply, $query ) {
foreach ( $reply->needFeedbacks as $index => $needFeedback ) {
$function = $needFeedback['function'];
if ( $function->id === 'userInfo' ) {
$value = call_userInfo();
if ( !empty( $value ) ) {
$reply->result = "Here is your data: " . $value;
unset( $reply->needFeedbacks[$index] );
return $reply;
}
}
}
return $reply;
}, 10, 2 );
Implementation Notes
- You can add this code to your theme's
functions.php
file or use a snippet tool like Snippet Vault.
- When declaring functions, use
Meow_MWAI_Query_Function::fromJson()
to define the function's properties.
- For functions with arguments, define them in the
args
array of the function definition.
- Choose either Method 1 or Method 2 for both declaring and handling function calls.