Backend Filters PHP
Interactions with AI
- mwai_ai_query( $query ) => Modify a query before it’s made
// NOTE: This filter does NOT apply to Embedding or Feedback queries (see below)
add_filter( 'mwai_ai_query', function( $query ) {
// Modify the query object
// Examples: change model, adjust temperature, add context
return $query;
});
- mwai_ai_instructions( $instructions ) => Modify the instructions (you can add the current day, time, information about the current user, etc)
// Modify AI instructions/context
add_filter( 'mwai_ai_instructions', function( $instructions, $query ) {
// Add or modify system instructions
$instructions .= "\nAlways be polite and helpful.";
return $instructions;
}, 10, 2 );
- mwai_ai_allowed( $allowed, $query, $limits ) => Allow a query to be made or not
// Control whether a query is allowed
add_filter( 'mwai_ai_allowed', function( $allowed, $query, $limits ) {
// Return true to allow, false to block, or a string error message
if ( /* some condition */ ) {
return 'Query blocked due to rate limits';
}
return $allowed;
}, 10, 3 );
- mwai_ai_reply( $rawText, $reply ) => Modify the answer before it’s sent (or do something at that time)
// Modify AI reply after it's received
add_filter( 'mwai_ai_reply', function( $rawText, $reply ) {
$query = $reply->query;
return $rawText;
}, 10, 2 );
- mwai_ai_exception( $message ) => Modify the error message
add_filter( 'mwai_ai_exception', function( $message ) {
// Hide technical details from users
if ( strpos( $message, 'API' ) !== false ) {
return 'Service temporarily unavailable. Please try again.';
}
return $message;
});
Chatbot
- mwai_chatbot_params( $params ) => Modify the final params just before the query is made
// Modify chatbot parameters
add_filter( 'mwai_chatbot_params', function( $params ) {
// Force specific settings
$params['temperature'] = 0.7;
$params['max_tokens'] = 500;
return $params;
});
- mwai_chatbot_takeover( $takeoverText, $query, $params )
// Takeover chatbot response (bypass AI)
add_filter( 'mwai_chatbot_takeover', function( $takeover, $query, $params ) {
// Check for specific triggers
if ( strpos( $query->get_message(), 'current time' ) !== false ) {
return 'The current time is ' . current_time( 'mysql' );
}
return $takeover;
}, 10, 3 );
- mwai_contentaware_content( $content ) => Handle extra use cases, like looking for content in specific fields, or handling page builders, etc
// Modify content before AI processing
add_filter( 'mwai_contentaware_content', function( $content, $post ) {
// Clean or enhance content
$content = strip_shortcodes( $content );
return $content;
}, 10, 2 );
- mwai_chatbot_reply( $rawText, $query, $params, $extra ) => Modify the answer before it’s sent.
// Process chatbot reply before sending to user
add_filter( 'mwai_chatbot_reply', function( $reply, $query, $params, $extra ) {
// Add custom formatting or processing
return $reply;
}, 10, 4 );
Tools
- mwai_ai_feedback_query( $query ): Modify feedback queries specifically (for function calling results). This is a Meow_MWAI_Query_Feedback object.
Embeddings
- mwai_pre_post_content( $content, $postId ): Get the raw content from the post. You can modify it to add your custom field, etc. AI Engine will clean that content further, and make sure it’s not too large for the AI model.
- mwai_post_content( $content, $postId ): The final content that will be used. You can also modify this.
// Same implementation for both filters mwai_pre_post_content and mwai_post_content
add_filter( 'mwai_pre_post_content', function( $content, $post_id ){
// Add any data you want AI Engine to get from your post.
$post_meta = get_post_meta( $post_id, '', true );
$custom_data = $post_meta['custom_data'];
$content .= "Custom Data: $custom_data."
return $content;
}, 10, 2 );
- mwai_ai_embeddings_query( $query ): Customize embedding queries before they’re sent to AI. This is not a regular Query object, this will be a Meow_MWAI_Query_Embed object.
// Modify embedding queries specifically (for semantic search, vector generation)
add_filter( 'mwai_ai_embeddings_query', function( $query ) {
// Modify embedding query settings
// Examples: change dimensions, switch embedding model, add metadata
$query->set_dimensions( 1536 );
return $query;
});
- mwai_context_search( $context, $query, $options): Add or modify context from embeddings
// Modify context search results
add_filter( 'mwai_context_search', function( $context, $query, $options ) {
// Add or modify context from embeddings
return $context;
}, 10, 3 );
Forms
- mwai_form_params( $params ) => Modify the final params
// Modify form parameters
add_filter( 'mwai_form_params', function( $params ) {
$params['submit_label'] = 'Generate Content';
return $params;
});
- mwai_form_reply( $rawText ) => Modify the answer before it’s sent
// Process form reply
add_filter( 'mwai_form_reply', function( $reply, $query, $params, $extra ) {
// Format or process the reply
return $reply;
}, 10, 4 );
Limits
- mwai_stats_credits( $credits, $userId ) => Modify the number of credits (that allows you to create your own system)
// Modify usage credits (e.g., different limits per model)
add_filter( 'mwai_stats_credits', function( $credits, $userId, $query ) {
// $query is available when checking limits before a query runs, null when displaying stats
if ( $query && $query->model === 'gpt-4o' ) {
return $credits * 2; // Double credits for GPT-4o
}
return $credits;
}, 10, 3 );
- mwai_stats_coins( $price, $stats, $atts ) => Modify the coins (that allows you to convert the price you personally pay to OpenAI to your own system of coins that you share with your users)
// Customize pricing calculations
add_filter( 'mwai_stats_coins', function( $price, $stats, $atts ) {
// Apply custom pricing logic
return $price;
}, 10, 3 );
- mwai_estimate_tokens( $tokens, $text ) => Modify the estimated number of tokens for the given text (you can modify the default algorithm) used to calculate the tokens usage estimation.
// Customize pricing calculations
add_filter( 'mwai_stats_coins', function( $price, $stats, $atts ) {
// Apply custom pricing logic
return $price;
}, 10, 3 );
Others
- mwai_languages( $languages ) => Modify the list of languages
- mwai_allow_setup( $hasAccess ) => Allow access to the Settings of AI Engine in the Admin
- mwai_allow_usage( $hasAccess ) => Allow access to the various screens of AI Engine in the Admin