Building an Elementor Add-On : Essential Code Snippets for Beginners

Catogories -

/ By -

Creating a WordPress plugin can seem daunting for beginners, but it’s actually a straightforward process. Here’s a step-by-step guide on how to create a Elementor add-on plugin:

#Set up your development environment: You’ll need a local server environment to build and test your plugin. You can use tools like XAMPP or WAMP to create a local server environment on your computer.

#Create a new folder for your plugin: In your WordPress plugin directory, create a new folder with a unique name for your plugin.

#Create a plugin file: Create a new file in your plugin folder and name it something like “plugin-name.php”. This will be the main file for your plugin.

#Add the plugin header information: In the top of your plugin file, add the plugin header information which includes the name, version, author, and other details about your plugin.


class Elementor_Addon {

// Constructor
public function __construct() {
add_action( 'elementor/widgets/widgets_registered', array( $this, 'register_widgets' ) );
}

// Register your widget
public function register_widgets() {
\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor\Widget_Addon() );
}
}

Create the class for your Elementor widget


class Widget_Addon extends \Elementor\Widget_Base {

// Widget Name
public function get_name() {
return 'widget-addon';
}

// Widget Title
public function get_title() {
return __( 'Widget Add-on', 'elementor-addon' );
}

// Widget Icon (optional)
public function get_icon() {
return 'eicon-editor-code';
}

// Widget Category
public function get_categories() {
return [ 'basic' ];
}

// Widget Content
protected function _content_template() {
?>
<div class="widget-addon">
<h3><?php echo esc_html( $settings['title'] ); ?></h3>
<p><?php echo esc_html( $settings['content'] ); ?></p>
</div>
<?php
}

// Widget Settings
protected function _register_controls() {
$this->start_controls_section(
'content_section',
[
'label' => __( 'Content', 'elementor-addon' ),
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);

$this->add_control(
'title',
[
'label' => __( 'Title', 'elementor-addon' ),
'type' => \Elementor\Controls_Manager::TEXT,
'default' => __( 'Widget Add-on Title', 'elementor-addon' ),
'placeholder' => __( 'Enter your title here', 'elementor-addon' ),
]
);

$this->add_control(
'content',
[
'label' => __( 'Content', 'elementor-addon' ),
'type' => \Elementor\Controls_Manager::WYSIWYG,
'default' => __( 'Widget Add-on Content', 'elementor-addon' ),
'placeholder' => __( 'Enter your content here', 'elementor-addon' ),
]
);

$this->end_controls_section();
}

// Widget Output
protected function render() {
$settings = $this->get_settings_for_display();

echo '<div class="widget-addon">';
echo '<h3>' . esc_html( $settings['title'] ) . '</h3>';
echo '<p>' . wp_kses_post( $settings['content'] ) . '</p>';
echo '</div>';
}
}

This basic code creates an Elementor add-on with a custom widget that displays a title and content. You can customize the widget and add more features as needed.

#Add the plugin functionality: Write the code to create the desired functionality of your plugin. This could be a shortcode, a custom post type, or anything else you want your plugin to do.

#Test your plugin: Once you’ve written your code, it’s time to test your plugin. Activate it in your WordPress dashboard and make sure it’s working as expected.

#Refine and publish your plugin: After testing, refine your code and fix any issues you find. Once you’re happy with your plugin, you can publish it to the WordPress plugin repository or sell it on your own website.