Title: Shortcode under Cookie condis
Last modified: November 27, 2023

---

# Shortcode under Cookie condis

 *  Resolved [malorkan](https://wordpress.org/support/users/malorkan/)
 * (@malorkan)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/shortcode-under-cookie-condis/)
 * is it possible to execute the shortcode only under the condition that a cookie
   is set beforehand?
   i use elementor and have tried back and forth to put it into
   an html or shortcode element using an if condition like this one
 *     ```wp-block-code
       <script>
       document.addEventListener('DOMContentLoaded', function() {
           // Überprüfe hier deine Bedingung
           if (document.cookie.indexOf('cmplz_preferences=allow') !== -1) {
               // Füge den Shortcode-Inhalt in das DOM ein
               var shortcodeContainer = document.getElementById('mwai_chatbot_v2');
               if (shortcodeContainer) {
                   shortcodeContainer.innerHTML = [mwai_chatbot_v2];
               }
           }
       });
       </script>
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Support [Val Meow](https://wordpress.org/support/users/valwa/)
 * (@valwa)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/shortcode-under-cookie-condis/#post-17234368)
 * Hey [@malorkan](https://wordpress.org/support/users/malorkan/) ! 👋
 * It might be easier to check for cookies and create a shortcode through the backend,
   as they are interpreted by WordPress. You could try something like this. 😊
 *     ```wp-block-code
       function custom_cookie_chatbot_shortcode_callback($atts) {
           // Check if the cookie is set
           if (isset($_COOKIE['cmplz_preferences'])) {
               // Render the mwai_chatbot_v2 shortcode content here
               return do_shortcode('[mwai_chatbot_v2]');
           } else {
               // Optionally, you can provide a message or alternative content
               return 'Cookie not set, chatbot shortcode not rendered';
           }
       }
       add_shortcode('cookie_mwai_chatbot_v2', 'custom_cookie_chatbot_shortcode_callback');
       ```
   
 *  Thread Starter [malorkan](https://wordpress.org/support/users/malorkan/)
 * (@malorkan)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/shortcode-under-cookie-condis/#post-17267812)
 * thanks for your quick reply. i had tried that but unfortunately the approach 
   did not work. can you describe how you can activate the shortcode through the
   backend? that i only activate the chatbot under the cookie condition on individual
   pages

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Shortcode under Cookie condis’ is closed to new replies.

 * ![](https://ps.w.org/ai-engine/assets/icon-256x256.png?rev=3431928)
 * [AI Engine - The Chatbot, AI Framework & MCP for WordPress](https://wordpress.org/plugins/ai-engine/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/ai-engine/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/ai-engine/)
 * [Active Topics](https://wordpress.org/support/plugin/ai-engine/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ai-engine/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ai-engine/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [malorkan](https://wordpress.org/support/users/malorkan/)
 * Last activity: [2 years, 6 months ago](https://wordpress.org/support/topic/shortcode-under-cookie-condis/#post-17267812)
 * Status: resolved