Generate a plugin for Elementor with this tool. It will create a plugin with a widget and a settings page. You can use it as a boilerplate for your next plugin.
/**
* Plugin Name: My Elementor Plugin
* Description: My Elementor Plugin Description
* Plugin URI:
* Version: 1.0
* Author: WP Skills
* Author URI: https://wp-skills.com
* Text Domain: text-domain
*/
final class WPSkills_Custom_Elementor_Plugin {
const VERSION = '1.0';
const MINIMUM_ELEMENTOR_VERSION = '2.0';
const MINIMUM_PHP_VERSION = '7.0';
private static $_instance = null;
/**
* Instance
*
* Ensures only one instance of the class is loaded or can be loaded.
*
* @since 1.0.0
* @access public
* @static
* @return WPSkills_Custom_Elementor_Plugin An instance of the class.
*/
public static function instance(): WPSkills_Custom_Elementor_Plugin {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Constructor
*
* Perform some compatibility checks to make sure basic requirements are meet.
* If all compatibility checks pass, initialize the functionality.
*
* @since 1.0.0
* @access public
*/
public function __construct() {
if ( $this->check_compatibility() ) {
add_action( 'elementor/init', array($this, 'init') );
}
}
/**
* Compatibility Checks
*
* Checks whether the site meets the addon requirement.
*
* @since 1.0.0
* @access public
*/
public function check_compatibility(): bool {
// Don't load this plugin if Elementor isn't installed or if hasn't been activated
if ( ! did_action( 'elementor/loaded' ) ) {
add_action( 'admin_notices', array($this, 'create_admin_notice_elementor_missing') );
return false;
}
// Check Elementor version
if ( ! version_compare( ELEMENTOR_VERSION, self::MINIMUM_ELEMENTOR_VERSION, '>=' ) ) {
add_action( 'admin_notices', array($this, 'create_admin_notice_elementor_wrong_version') );
return false;
}
// Check PHP version
if ( version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '<' ) ) {
add_action( 'admin_notices', array($this, 'create_admin_notice_wrong_php_version') );
return false;
}
return true;
}
/**
* Admin notice
*
* Warning when the site doesn't have Elementor installed or activated.
*
* @since 1.0.0
* @access public
*/
public function create_admin_notice_elementor_missing() : void {
$message = '<div class="notice notice-warning is-dismissible">';
$message .= "<p><strong>My Awesome Plugin</strong> requires <strong>Elementor</strong> plugin to work. Please install and activate the Elementor plugin.</p>";
$message .= '</div>';
echo $message;
}
/**
* Admin notice
*
* Warning when the site doesn't have a minimum required Elementor version.
*
* @since 1.0.0
* @access public
*/
public function create_admin_notice_elementor_wrong_version() : void {
$message = '<div class="notice notice-warning is-dismissible">';
$message .= "<p><strong>My Awesome Plugin</strong> requires <strong>Elementor</strong> version 2.0 or higher.</p>";
$message .= '</div>';
echo $message;
}
/**
* Admin notice
*
* Warning when the site doesn't have a minimum required PHP version.
*
* @since 1.0.0
* @access public
*/
public function create_admin_notice_wrong_php_version() : void {
$message = '<div class="notice notice-warning is-dismissible">';
$message .= "<p><strong>My Awesome Plugin</strong> requires <strong>PHP</strong> version 7.0 or higher.</p>";
$message .= '</div>';
echo $message;
}
/**
* Initialize
*
* Load the addons functionality only after Elementor is initialized.
*
* Fired by `elementor/init` action hook.
*
* @since 1.0.0
* @access public
*/
public function init() : void {
$this->i18n();
add_action( 'elementor/widgets/widgets_registered', array($this, 'init_widgets') );
add_action( 'elementor/controls/controls_registered', array($this, 'init_controls') );
}
/**
* Load Textdomain
* Load plugin localization files.
* @access public
*/
public function i18n() : void {
load_plugin_textdomain('WPSkills');
}
/**
* Register Widgets
*
* Load widgets files and register new Elementor widgets.
*
* Fired by `elementor/widgets/register` action hook.
*
* @param \Elementor\Widgets_Manager $widgets_manager Elementor widgets manager.
*/
public function init_widgets( \Elementor\Widgets_Manager $widgets_manager ) : void {
// This is wheere you can include your widget files.
// require_once( __DIR__ . '/widgets/my-widget.php' );
// Register your widget after including it.
// ElementorPlugin::instance()->widgets_manager->register_widget_type( new Your_Elementor_Widget_Class_Name() );
}
/**
* Register Controls
*
* Load controls files and register new Elementor controls.
*
* Fired by `elementor/controls/register` action hook.
*
* @param \Elementor\Controls_Manager $controls_manager Elementor controls manager.
*/
public function init_controls( \Elementor\Controls_Manager $controls_manager ) : void {
// This is where you can include your control files.
// require_once( __DIR__ . '/controls/my-controls.php' );
// Register your control after including it.
// ElementorPlugin::$instance->controls_manager->register_control( 'control-type-', new Your_Control_Class_Name() );
}
}
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
function wp_skills_load_elementor_plugin() : void {
WPSkills_Custom_Elementor_Plugin::instance();
}
add_action( 'plugins_loaded', 'wp_skills_load_elementor_plugin' );