Display The Chatbot Under Conditions
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 two scenarios: checking a specific cookie and verifying user rights.
Example 1: Check for a Specific Cookie
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 2: 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.