KYSSE
KYSSE

Reputation: 385

Hook into elementor widget?

I am trying to find a hook that will let me add my own code into an existing elementor widget. For example they have the "post widget" which lets you display a list of posts based on the conditions/categories you set.

I would like to add my own code into this "block" but am unable to find any specific hooks for hooking into an existing widget (specifically the posts widget)

Any help would be much appreciated. Is there a hook for this? If not what is my next best option?

Thanks!

Upvotes: 1

Views: 4249

Answers (1)

Insomnia88
Insomnia88

Reputation: 368

This depends on what you want to achieve but in general there are hooks.

I am not sure about the posts-widget but I can show you some examples in general.

If you want to add controls to a widget use this (you can find additional information to the names and stuff in their documentation https://developers.elementor.com/add-controls-to-widgets/)

add_action( 'elementor/element/heading/section_title/before_section_end', function( $element, $args ) {
    $element->add_control( 'title_color',
        [
            'label' => 'Color' ,
            'type' => \Elementor\Controls_Manager::SELECT,
            'default' => 'red',
            'options' => [
                'red' => 'Red',
                'blue' => 'Blue',
            ],
            'section' => 'section_title',
            'tab' => 'content',
        ]
    );
}, 10, 2);

the widget in the example is the heading. You can find out the registered names by inspecting the editor or the blocks inside the plugin directory

If you want to change the rendered content of a widget you can use this

add_action( 'elementor/widget/render_content', function( $content, $widget ){ // $content (string) = the rendered content of the widget; widget = the data object 
    if ($widget->get_name() === 'heading') { // your targeted widget
        $settings = $widget->get_settings(); // every that is stored in this method. Titles, captions, margins and so on. Usually this is all you need
        // eg if you simply want to wrap your widgets content you can do something like this
        $content .= '<div class="i-am-a-wrapper">'.$content.'</div>';
    }
    return $content;
}, 10, 2 );

I hope this helps :)

Upvotes: 1

Related Questions