5 min read 2 wks ago

Shortcode Overwrite

Display The Chatbot Under Conditions.

If you have a chatbot on your page that you want to display only under certain conditions (such as membership, user role, or time of day), you can create your own shortcode with custom logic that conditionally calls the AI Engine chatbot shortcode.

This guide will walk you through the process of creating a shortcode in WordPress that displays content only when specific conditions are met, in this case AI Engine’s chatbot shortcode. We’ll cover 4 scenarios: a different chatbot for each page, checking a specific cookie, verifying user rights and limiting the chatbot to certain hours.

Example 1: Different Chatbot for each Page

Let’s create a WordPress shortcode that displays a different AI Engine chatbot on each page depending on the page’s slug, and ensure this shortcode is executed in the footer of every page, so this way we don’t have to manually go and place our custom shortcode on each post manually.

// Create our custom shortcode
function page_slug_chatbot_shortcode_callback($atts) {
    // Get the global post object
    global $post;

    if (!$post) {
        return ''; // No post context
    }

    // Get the page slug
    $slug = $post->post_name;

    // Map page slugs to chatbot IDs (the IDs of chatbots created in AI Engine)
    $custom_bots = [
        'home' => 'homepage_bot',
        'contact' => 'contact_bot',
        'about-us' => 'about_bot',
        'blog' => 'blog_assistant'
    ];

    // Fallback for the other pages
    $default_id = 'default';

    // Use the mapped ID if defined, else the default chatbot
    $chatbot_id = $custom_bots[$slug] ?? $default_id;

    return do_shortcode('[mwai_chatbot id="' . esc_attr($chatbot_id) . '"]');
}
add_shortcode('slug_mwai_chatbot', 'page_slug_chatbot_shortcode_callback');

// Add the shortcode on every posts
function insert_chatbot_shortcode_in_footer() {
    echo do_shortcode('[slug_mwai_chatbot]');
}
add_action('wp_footer', 'insert_chatbot_shortcode_in_footer');
function custom_cookie_chatbot_shortcode_callback($atts) {
    // Check if the custom_cookie is set
    if (isset($_COOKIE['custom_cookie'])) {
        // Render the mwai_chatbot shortcode here
        return do_shortcode('[mwai_chatbot]');
    } else {
        // Optionally, provide a message or alternative content
        return 'Custom cookie not set, chatbot shortcode not rendered';
    }
}
add_shortcode('cookie_mwai_chatbot', 'custom_cookie_chatbot_shortcode_callback');

Add the [cookie_mwai_chatbot] shortcode to your WordPress post or page. The enclosed content will only display if the ‘custom_cookie’ is set.

Example 3: Check User Rights

function user_rights_chatbot_shortcode_callback($atts) {
    // Check if the user has specific rights (e.g., 'manage_options')
    if (current_user_can('manage_options')) {
        // Render the mwai_chatbot shortcode here
        return do_shortcode('[mwai_chatbot id="manager_bot"]');
    } else {
        // Optionally, return the default chatbot
        return do_shortcode('[mwai_chatbot]');
    }
}
add_shortcode('rights_mwai_chatbot', 'user_rights_chatbot_shortcode_callback');

Add the [rights_mwai_chatbot] shortcode to your WordPress post or page. Users who have the rights to manage options will experience a specific chatbot tailored for them, while regular users will see your default chatbot in the same location.

The possibilities are endless; you can even create a customized “mwai” shortcode with parameters based on users’ preferences if needed. You can add custom logic, retrieve data from an external API, and the conditions can be tailored to virtually anything.


Example 4: Display Chatbot Only Between 8 AM and 8 PM

If your server is in a different timezone than your intended audience, use date_default_timezone_set() to ensure accuracy.

You could expand this logic to display different bots for different times (e.g., daytime vs. nighttime assistant), or include logic for weekends, holidays, or even integrate with an external calendar API.

function time_restricted_chatbot_shortcode_callback($atts) {
    // Set your desired timezone
    date_default_timezone_set('UTC'); // Adjust to your local timezone if needed, e.g., 'Europe/Paris'

    // Get the current hour (24-hour format)
    $current_hour = (int) date('G'); // Returns 0 to 23

    // Define start and end hour for chatbot display
    $start_hour = 8;
    $end_hour = 20;

    if ($current_hour >= $start_hour && $current_hour < $end_hour) {
        // Within allowed time range
        return do_shortcode('[mwai_chatbot]');
    } else {
        // Outside allowed time range
        return 'The chatbot is available between 8:00 AM and 8:00 PM.';
    }
}
add_shortcode('time_mwai_chatbot', 'time_restricted_chatbot_shortcode_callback');

Good to know: changing settings through the shortcode

The examples above only choose which chatbot to show, which always works. If you also pass settings to the shortcode, keep in mind that the sensitive ones (instructions, model, environment, API key and the other AI settings) are restricted for security since AI Engine 3.8.4:

  • In a post or page, they only apply when the author of that post can edit others’ posts (Editors and Administrators). For other authors, they are ignored and the chatbot keeps its own settings.
  • Outside post content (your theme code, like in Example 1, widgets or templates), they work on a normal page view. But once that custom chatbot is live, changing its settings only takes effect when someone who can post unfiltered HTML (usually an Administrator) views the page, or after 24 hours.
  • Visual settings (texts, avatars, theme, window options) are not concerned.

If you need to change AI settings from code, the mwai_chatbot_params filter is the reliable way: it runs on your server for every message.