Created
June 25, 2023 20:24
-
-
Save paperbonsai/ec6eaedb1a1ce313f040976d300f209f to your computer and use it in GitHub Desktop.
This code defines a custom widget for the Elementor page builder plugin in WordPress, which extends Elementor's existing Shortcode widget. The purpose of this custom widget is to provide a more convenient way for users to insert predefined shortcodes while editing their pages with Elementor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('elementor/elements/elements_registered', function () { | |
if (!\Elementor\Plugin::$instance->editor->is_edit_mode()) { | |
return; | |
} | |
class Extended_Shortcode_Widget extends \Elementor\Widget_Shortcode | |
{ | |
public function get_name() | |
{ | |
return 'shortcode'; | |
} | |
protected function _register_controls() | |
{ | |
parent::_register_controls(); | |
$shortcode_tags = [ | |
'[shortcode1 param1="value1" param2="value2"]', | |
'[shortcode2 param1="value1" param2="value2"]', | |
'[shortcode3 param1="value1" param2="value2"]', | |
'[shortcode4 param1="value1" param2="value2"]' | |
]; | |
$shortcode_list = '<ul><li class="sc_item">' . implode('</li><li class="sc_item">', $shortcode_tags) . '</li></ul>'; | |
$this->start_controls_section( | |
'section_shortcode', | |
[ | |
'label' => __('My Shortcodes', 'plugin-domain'), | |
'tab' => \Elementor\Controls_Manager::TAB_CONTENT, | |
'condition' => [], | |
] | |
); | |
$this->add_control( | |
'my_custom_control', | |
[ | |
'type' => \Elementor\Controls_Manager::RAW_HTML, | |
'raw' => $shortcode_list, | |
'content_classes' => ' e-open', | |
] | |
); | |
$this->end_controls_section(); | |
} | |
} | |
require_once ELEMENTOR_PATH . 'includes/base/controls-stack.php'; | |
require_once ELEMENTOR_PATH . 'includes/base/widget-base.php'; | |
require_once ELEMENTOR_PATH . 'includes/widgets/shortcode.php'; | |
$extended_widget = new Extended_Shortcode_Widget(); | |
\Elementor\Plugin::instance()->widgets_manager->register_widget_type($extended_widget); | |
?> | |
<style> | |
.sc_item { | |
cursor: pointer; | |
border-bottom: 1px solid orange; | |
padding: 5px 0; | |
} | |
.sc_item:hover { | |
color: orange; | |
} | |
li:last-child { | |
border-bottom: 0; | |
} | |
</style> | |
<script> | |
document.addEventListener('DOMContentLoaded', function() { | |
document.addEventListener('click', function(e) { | |
if (!e.target.classList.contains('sc_item')) { | |
return; | |
} | |
e.preventDefault(); | |
var clickedShortcode = e.target.textContent; | |
var textarea = document.querySelector('.elementor-control-tag-area'); | |
if (!textarea) { | |
alert('Textarea not found.'); | |
return; | |
} | |
if (getComputedStyle(textarea).display === 'none') { | |
alert('Textarea is not visible.'); | |
return; | |
} | |
textarea.value = clickedShortcode; | |
}); | |
}); | |
</script> | |
<?php | |
}, 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment