JavaScript API Reference
JavaScript API Reference
The wpai plugin exposes global JavaScript functions that allow you to programmatically control the chatbot interface. These functions are useful for triggering the chatbot from custom navigation menus, buttons, or other interactive elements on your WordPress site.
togglePopup()
The togglePopup() function switches the visibility of the chatbot interface. If the chatbot is currently hidden, calling this function will show it; if it is already open, the function will hide it.
Parameters:
- None
Return Value:
void
Example Usage:
You can call this function directly from an HTML onclick attribute to create a custom toggle button:
<button type="button" onclick="togglePopup()">
Need Help? Chat Now
</button>
Alternatively, you can call it from your own script file:
// Example: Open the bot after a 5-second delay
setTimeout(() => {
togglePopup();
}, 5000);
closePopup()
The closePopup() function explicitly hides the chatbot interface, regardless of its current state.
Parameters:
- None
Return Value:
void
Example Usage:
This is useful if you want to ensure the chatbot is closed when a user performs a specific action, such as clicking a "Close" button in a custom header.
document.getElementById('my-custom-close-btn').addEventListener('click', function() {
closePopup();
console.log('Chatbot has been closed.');
});
Integration Requirements
For these functions to interact correctly with the chatbot:
- Shortcode Presence: Ensure the
[supervised_ai_bot]shortcode is present on the page. - Element ID: The functions specifically target an HTML element with the ID
popup. The plugin generates this container automatically when the shortcode is used. - Styles: The visibility is controlled by toggling the
displayproperty betweenblockandnone. Ensure no high-priority CSS (like!important) overrides these transitions if you are using custom stylesheets.