Skip to content

Instantly share code, notes, and snippets.

@mariusgnicula
Created October 11, 2017 07:33
Show Gist options
  • Save mariusgnicula/2261bb9548ef1a5388704bf0ef02528b to your computer and use it in GitHub Desktop.
Save mariusgnicula/2261bb9548ef1a5388704bf0ef02528b to your computer and use it in GitHub Desktop.
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
Copy link

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