Elementor is built to be extended. Behind the drag-and-drop editor sit hooks that let you register your own widgets, group them in a custom category, change what existing widgets output and add controls to them. This guide covers the ones you’ll use first. The complete list is in our Elementor hooks reference, which updates with every release.
Key takeaways
- Register widgets on
elementor/widgets/register; older tutorials use names that are now deprecated. - Add your own panel category with
elementor/elements/categories_registered. elementor/widget/render_contentchanges any widget’s front-end output.- Keep your Elementor code in its own small plugin so it survives theme changes.
1. Register a custom widget
A widget is a class extending \Elementor\Widget_Base. Register it on elementor/widgets/register, which passes the widgets manager:
add_action( 'elementor/widgets/register', function ( $widgets_manager ) {
require_once __DIR__ . '/widgets/class-opening-hours.php';
$widgets_manager->register( new \My_Site\Opening_Hours_Widget() );
} );And a minimal widget class:
<?php
namespace My_Site;
defined( 'ABSPATH' ) || exit;
class Opening_Hours_Widget extends \Elementor\Widget_Base {
public function get_name() {
return 'my-opening-hours';
}
public function get_title() {
return __( 'Opening hours', 'my-site' );
}
public function get_icon() {
return 'eicon-clock-o';
}
public function get_categories() {
return array( 'my-site' ); // Our category, registered below.
}
protected function register_controls() {
$this->start_controls_section( 'content', array( 'label' => __( 'Content', 'my-site' ) ) );
$this->add_control( 'hours', array(
'label' => __( 'Hours', 'my-site' ),
'type' => \Elementor\Controls_Manager::TEXTAREA,
'default' => "Mon-Fri 9:00-17:00\nSat 10:00-14:00",
) );
$this->end_controls_section();
}
protected function render() {
$settings = $this->get_settings_for_display();
echo '<div class="opening-hours">' . nl2br( esc_html( $settings['hours'] ) ) . '</div>';
}
}If you’re following an older tutorial, you may see elementor/widgets/widgets_registered and register_widget_type(). Those are the deprecated names; use the ones above.
2. Add your own widget category
Group your widgets in the editor panel with elementor/elements/categories_registered, which passes the elements manager:
add_action( 'elementor/elements/categories_registered', function ( $elements_manager ) {
$elements_manager->add_category( 'my-site', array(
'title' => __( 'My Site', 'my-site' ),
'icon' => 'fa fa-plug',
) );
} );3. Load your CSS only with Elementor’s
elementor/frontend/after_enqueue_styles runs right after Elementor adds its front-end styles, so your stylesheet loads in the right order and only where Elementor is used:
add_action( 'elementor/frontend/after_enqueue_styles', function () {
wp_enqueue_style( 'my-site-elementor', plugins_url( 'assets/elementor.css', __FILE__ ), array(), '1.0.0' );
} );For scripts, register them on elementor/frontend/after_register_scripts and declare them in your widget’s get_script_depends(), so they load only on pages that use the widget.
4. Change the output of any widget
elementor/widget/render_content filters each widget’s HTML on the front end, with the widget object as the second argument. Add rel="nofollow" to links in every Button widget:
add_filter( 'elementor/widget/render_content', function ( $content, $widget ) {
if ( 'button' === $widget->get_name() ) {
$content = str_replace( '<a ', '<a rel="nofollow" ', $content );
}
return $content;
}, 10, 2 );5. Add a control to an existing widget
elementor/element/after_section_end runs after every controls section of every element, with the element, the section ID and its arguments. Add a toggle at the end of the Heading widget’s title section:
add_action( 'elementor/element/after_section_end', function ( $element, $section_id, $args ) {
if ( 'heading' !== $element->get_name() || 'section_title' !== $section_id ) {
return;
}
$element->start_controls_section( 'my_extra', array( 'label' => __( 'Extra', 'my-site' ) ) );
$element->add_control( 'my_highlight', array(
'label' => __( 'Highlight', 'my-site' ),
'type' => \Elementor\Controls_Manager::SWITCHER,
'prefix_class' => 'is-highlighted-', // Adds a CSS class to the widget wrapper.
) );
$element->end_controls_section();
}, 10, 3 );Section IDs differ per widget. Find them by browsing the widget’s class in Elementor’s source, or look the hook up in our reference to see where it fires.
More Elementor hooks
elementor/init: Elementor has loaded; safe place to check for it.elementor/controls/register: register a custom control type.elementor/dynamic_tags/register: add your own dynamic tags.elementor/editor/after_enqueue_scripts: load scripts inside the editor only.
All of them, with arguments and source locations, are in the Elementor hooks reference. New to hooks? Read WordPress hooks explained first. Need a custom Elementor widget built? Tell us what it should do.
Leave a Reply