aiwoocommerce.nl Free AI audit
mu-pluginwordpresshooks

Build a tiny WordPress mu-plugin AI toolkit for your WooCommerce store

How to ship a 50-line mu-plugin that gives your team an OpenAI helper, an AI helpdesk-draft hook and a review classifier — in one file you can drop into wp-content/mu-plugins.

Max van Kuik

Most AI tooling for WooCommerce assumes you’ll wire something external — n8n, Make, Zapier, or a third-party AI app. That’s the right call most of the time. But for stores that already have technical capability and want minimum dependencies, a tiny WordPress mu-plugin can carry surprising weight.

This article walks through a 50-line mu-plugin that ships three concrete AI capabilities directly inside your WordPress instance:

  1. A reusable OpenAI helper function
  2. An AI-drafted helpdesk reply on new tickets (works with Fluent Support; adaptable)
  3. A review classifier that tags reviews automatically

If you’re more comfortable with n8n flows than PHP, skip this — the workflows from woocommerce-n8n-ai-stack cover the same ground without writing PHP. This is for operators who like having things inside WordPress.

What is a mu-plugin

WordPress has three plugin types:

  • Plugins — installed via the admin UI, can be activated/deactivated.
  • Drop-ins — special files like db.php for custom DB drivers.
  • Mu-plugins (“must-use”) — files in wp-content/mu-plugins/ that auto-load on every WordPress request, no activation needed.

Mu-plugins are perfect for site-specific code that isn’t a “real” plugin: utilities, hooks, automations. They live with the site, can’t be accidentally deactivated, and sync via Git.

Setup

  1. Create the directory if it doesn’t exist:
mkdir -p wp-content/mu-plugins
  1. Add your OpenAI API key to wp-config.php (NOT in the mu-plugin file):
define( 'OPENAI_API_KEY', 'sk-...' );
  1. Create the mu-plugin file: wp-content/mu-plugins/aiwoo-toolkit.php.

The complete mu-plugin (50 lines)

<?php
/**
 * Plugin Name: AIWoo Toolkit
 * Description: Tiny AI helpers for this WooCommerce store.
 * Version: 0.1.0
 */

if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Reusable OpenAI helper. Returns the assistant message string,
 * or a WP_Error on failure.
 */
function aiwoo_openai( $prompt, $model = 'gpt-4o-mini' ) {
    if ( ! defined( 'OPENAI_API_KEY' ) || ! OPENAI_API_KEY ) {
        return new WP_Error( 'no_key', 'OPENAI_API_KEY missing in wp-config.php' );
    }

    $response = wp_remote_post( 'https://api.openai.com/v1/chat/completions', [
        'timeout' => 20,
        'headers' => [
            'Authorization' => 'Bearer ' . OPENAI_API_KEY,
            'Content-Type'  => 'application/json',
        ],
        'body' => wp_json_encode( [
            'model'    => $model,
            'messages' => [ [ 'role' => 'user', 'content' => $prompt ] ],
        ] ),
    ] );

    if ( is_wp_error( $response ) ) return $response;
    $body = json_decode( wp_remote_retrieve_body( $response ), true );
    return $body['choices'][0]['message']['content'] ?? '';
}

/**
 * Helpdesk hook: when a new Fluent Support ticket arrives, generate
 * an AI draft reply and save it as private note metadata.
 */
add_action( 'fluent_support/ticket_created', function( $ticket ) {
    $prompt = "You are customer support at {$ticket->mailbox_id}.\n"
            . "Draft a reply (max 120 words) to:\n\n\"{$ticket->content}\"\n\n"
            . "Tone: clear, helpful. End with one concrete next step.";
    $draft = aiwoo_openai( $prompt );
    if ( ! is_wp_error( $draft ) ) {
        update_post_meta( $ticket->id, '_ai_draft_reply', $draft );
    }
}, 20, 1 );

/**
 * Review classifier: when a new product review is approved, tag with
 * AI-derived sentiment + topic.
 */
add_action( 'comment_post', function( $comment_id ) {
    $comment = get_comment( $comment_id );
    if ( $comment->comment_type !== 'review' ) return;

    $prompt = "Classify this review.\nText: \"{$comment->comment_content}\"\n\n"
            . "Output JSON: { \"sentiment\": \"positive|neutral|negative\", "
            . "\"topic\": \"product|delivery|service|pricing|other\" }.";
    $resp = aiwoo_openai( $prompt );
    if ( is_wp_error( $resp ) ) return;

    $data = json_decode( trim( $resp, "` \n" ), true );
    if ( ! empty( $data ) ) {
        update_comment_meta( $comment_id, '_ai_sentiment', $data['sentiment'] );
        update_comment_meta( $comment_id, '_ai_topic',     $data['topic'] );
    }
}, 10, 1 );

That’s the entire toolkit. Drop it in, restart no servers required (mu-plugins auto-load), and start prompting.

How you use this in practice

From anywhere in WordPress code

$summary = aiwoo_openai("Summarize this customer email in 1 sentence: {$email}");

Display the helpdesk draft in your support UI

If you’re using Fluent Support, add a small UI hook to render _ai_draft_reply meta in the ticket sidebar. Two-line snippet, but adapt to your helpdesk’s templating.

Filter reviews by sentiment

In the WP Admin → Comments view, filter by the _ai_sentiment meta to see all negative reviews from the past 30 days. Build a small admin widget or use a plugin like Admin Columns.

What to know before shipping

  • Synchronous AI calls inside hooks block the request. That’s why the helpdesk hook above runs on ticket_created (after the customer has submitted) and the review classifier runs on comment_post (after the comment is saved). Never put synchronous AI calls in checkout-blocking hooks.

  • For heavier or batch tasks, use Action Scheduler. WooCommerce ships with Action Scheduler. Schedule async AI jobs:

as_schedule_single_action( time() + 5, 'aiwoo/process_review', [ $comment_id ] );

add_action( 'aiwoo/process_review', function( $comment_id ) {
    // your AI call here
});

This decouples the AI call from the request that triggered it. Bonus: built-in retry on failure.

  • Logging. When aiwoo_openai() returns a WP_Error, log it. Either to WordPress’s error_log or to a custom comment-meta-style audit log:
$result = aiwoo_openai( $prompt );
if ( is_wp_error( $result ) ) {
    error_log( '[aiwoo] ' . $result->get_error_message() );
    return;
}
  • Privacy. This sends customer data (review text, ticket content) to OpenAI. For GDPR-sensitive cases use OpenAI’s API with the data-policy that excludes training, or run a local model via Ollama and replace the API URL.

Where to extend

If you find this useful, the natural extensions are:

  1. Customer-facing tag generator — auto-generate WordPress post tags / WooCommerce product tags using AI for newly-created products.
  2. Daily digest — wp-cron triggers a daily AI summary of orders/reviews/support tickets and emails it.
  3. Image alt text — hook into add_attachment for product images, generate descriptive alt text via OpenAI Vision.
  4. AI-flagged orders — hook into woocommerce_payment_complete for fraud screening (see WordPress hooks + AI use cases for full pattern — though that’s a Dutch-language blog at aiwoocommerce.nl, the structure of the pattern translates).

When to graduate to n8n

This toolkit shines for 1-2 simple use cases. Once you need:

  • Cross-system orchestration (e.g. WooCommerce → Klaviyo → Slack → Notion)
  • Visual flow editing for non-developers
  • Built-in retry, branching, error handling
  • Multiple AI models (Claude + OpenAI)
  • Better observability of which workflows ran when

…graduate to n8n. The mu-plugin handles light tasks; n8n handles serious orchestration.

Want a more comprehensive toolkit?

Book a free WooCommerce AI audit — we’ll review your current setup and ship a tailored mu-plugin or n8n flow set as part of the implementation phase.

Ready to put AI to work in your store?

Book a free 60-minute AI audit. You'll walk away with the five highest-leverage AI moves for your store — no commitments.

Related reading