Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to include JS into custom Elementor Widget
Include your JS into a normal file, just how you are used to and enqueue it with wp_enqueue_script. This will not work in the editor, because you want the functions to fire when you add them.
At the beginning of your widget file, add ‘use Elementor\Plugin;’.
Then at the end of the render() function in your PHP, add this:
<?php if ( Plugin::$instance->editor->is_edit_mode() ) : ?>
<script>Your code here</script
<?php
This will only load this section if you are in the editor. The sad thing is that you will have some duplicate code, but so far I've not found a better way. Include only what you need for that particular widget.
Hope this helps!
Have a great day.
@latiosthinh

This comment has been minimized.

Copy link

@latiosthinh latiosthinh commented Jul 19, 2019

Excuse me. What does the "Plugin" stand for ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment