3 min read 1 day 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 based on 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’ chatbot shortcode. We’ll cover 3 scenarios: a site wide post specific chatbot, checking a specific cookie and verifying user rights.

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;

    // Use the slug as the chatbot ID (or map to specific values if needed)
    $chatbot_id = esc_attr($slug); // ensure safety

    // You can also define a fallback default bot if needed
    $default_id = 'default_bot';

    // Optionally, define custom mappings
    $custom_bots = [
        'home' => 'homepage_bot',
        'contact' => 'contact_bot',
        'about-us' => 'about_bot',
        'blog' => 'blog_assistant'
    ];

    // Use mapped ID if defined, else fallback to slug or default
    $chatbot_id = $custom_bots[$slug] ?? $chatbot_id ?? $default_id;

    return do_shortcode('[mwai_chatbot custom_id="' . $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_v2 shortcode content 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_v2 shortcode content 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.