How to create a widget in Local?

How to create a widget in Local?

Hello @RicardoRocha - welcome to the Local Community! :wave:

Thank you so much for your question - can you provide a little more context, please?

Are you looking to create a Widget inside of your WordPress site or do something within Local (like create an Add-On)?

Best,

Sam :woman_technologist:t3:

I have two files one plugin-b2w.php with

<?php /** * * @package B2W * * Plugin Name: Plugin For Bootstrap to WordPress * Description: Custom Elementor Widgets for our Bootstrap to WordPress course * Plugin URI: https://bootstrapwordpress.com * Version: 1.0.0 * Author: Brad Hussey * Author URI: https://bradhussey.ca * Text Domain: plugin-b2w */ if ( ! defined('ABSPATH') ) { exit; //Exit if accessed directly. } $plugin_images = plugin_dir_url(__FILE__). 'assets/images'; /** * Main B2W Elementar Extension Class * * The main class that initiates and runs the plugin * * @since 1.0.0 */ final class B2W_Elementor_Extension { /** * Plugin Version * * @since 1.0.0 * * @var string The plugin version */ const VERSION = '1.0.0'; /** * Minimum Elementor Version * * @since 1.0.0 * * @var string Minimum Elementor version required to run the plugin */ const MINIMUM_ELEMENTOR_VERSION = '3.2.4'; /** * Minimum PHP Version * * @since 1.0.0 * * @var string Minimum PHP version required to run the plugin */ const MINIMUM_PHP_VERSION = '7.0'; /** * Instance * * @since 2.0.0 * * @access private * @static * * @var B2W_Elementor_Extension The single instance of the class * * When this fires up, initially set the instance as null. * */ private static $_instance = null; /** * Instance * * Ensures only one instance of the class is loaded or can be loaded. * * @since 2.0.0 * * @access private * @static * * @var B2W_Elementor_Extension An instance of the class * */ public static function instance() { if(is_null(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; } /** * Construtor * * @since 1.0.0 * * @access public */ public function __construct () { add_action('plugins_loaded', [ $this, 'on_plugins_loaded']); } /** * Load TextDomain * * Load plugin localization files * * Fired by init action hook * * @since 1.0.0 * *@access public */ public function i18n() { load_plugin_textdomain('plugin-b2w'); } /** * On Plugin Loaded * * Checks if Elementor has loaded, and performs some compatibility checks * If All Checks pass, inits the plugin. * * Fired by plugin_loaded action hook. * * @since 1.0.0 * * @access public */ public function on_plugins_loaded() { if( $this->is_compatible) { add_action('elementor/init', [ $this, 'init']); } } /** * Compatibility Checks * * Checks if the installed version of ELementor meets the plugin's minimum requirement. * Checks if the installed PHP version meets the plugin's minimum reuirement. * * @since 1.0.0 * * @access public */ public function is_compatible() { //Check if Elementar installed and activated if ( !did_action('elementor/loaded')) { add_action('admin_notices', [ $this, 'admin_notice_missing_main_plugin']); return false; } //check for reqired Elementor version if ( !version_compare(ELEMENTOR_VERSION, self::MINIMUM_ELEMENTOR_VERSION, '>=')) { add_action( 'admin_notices', [ $this, 'admin_notice_minimum_elementor_version']); return false; } //check for reqired PHP version if ( version_compare(PHP_VERSION, self::MINIMUM_PHP_VERSION, '<')) { add_action('admin_notices', [ $this, 'admin_notice_minimum_php_version']); return false; } return true; } /** * Initialize the plugin * * Load the plugin only after Elementor (and other plugins) are loaded. * Load the files required to run the plugin. * * Fired by 'plugins_loaded' action hook. * * @since 1.0.0 * * @access public */ public function init() { $this->i18n(); //Add Plugin actions add_action('elementor/elements/categories_registered'. array( $this. 'add_elementor_widget_categories')); add_action('elementor/widgets/widgets_registered'. [ $this. 'init_widgets']); } /** * Adding a custom category */ public function add_elementor_widget_categories($elements_manager) { $elements_manager->add_category ( 'b2w_category', [ 'title' => __('Bootstrap to WordPress', 'plugin-b2w'), 'icon' => 'eicon-nerd', ] ); } /** * Admin notice * * warning when the site doesn't have Elementor installed or Activated. * * @since 1.0.0 * * @access public */ public function admin_notice_missing_main_plugin() { if ( isset ($_GET['activate'])) unset($_GET['activate']); $message = sprintf( /* translaters: 1:Plugin name 2: Elementor */ esc_html__(' %1$s" requires "%2$s" to be installed and activated.', 'b2w-elementor-extension'), '' . esc_html__('B2W Elementor Extension', 'b2w-elementor-extension') . '', '' . esc_html__('Elementor', 'b2w-elementor-extension') . '' ); printf( '

%1$s

', $message ); } /** * Admin notice * * warning when the site doesn't have a minimum required Elementor version * * @since 1.0.0 * * @access public */ public function admin_notice_missing_elementor_version() { if ( isset ($_GET['activate'])) unset($_GET['activate']); $message = sprintf( /* translaters: 1:Plugin name 2: Elementor 3: Required Elementor version */ esc_html__(' "%1$s" requires "%2$s" version "%3$s" or greater.', 'b2w-elementor-extension'), '' . esc_html__('B2W Elementor Extension', 'b2w-elementor-extension') . '', '' . esc_html__('Elementor', 'b2w-elementor-extension') . '', self::MINIMUM_ELEMENTOR_VERSION ); printf( '

%1$s

', $message ); } /** * Admin notice * * warning when the site doesn't have a minimum required PHP version * * @since 1.0.0 * * @access public */ public function admin_notice_missing_php_version() { if (isset ($_GET['activate'])) unset($_GET['activate']); $message = sprintf( /* translaters: 1:Plugin name 2: Elementor 3: Reuired PHP version*/ esc_html__(' "%1$s" requires "%2$s" version "%3$s" or greater.', 'b2w-elementor-extension'), '' . esc_html__('B2W Elementor Extension', 'b2w-elementor-extension') . '', '' . esc_html__('PHP', 'b2w-elementor-extension') . '', self::MINIMUM_PHP_VERSION ); printf( '

%1$s

', $message ); } /** * Init Widgets * * Include Widgets files and register then * * @since 1.0.0 * * @access public */ public function init_widgets() { //include widgets files require_once(__DIR__. '/widgets/class-buttons.php'); // require_ince(__DIR__. '/widgets/class-title.php'); // require_ince(__DIR__. '/widgets/class-color-link.php'); // require_ince(__DIR__. '/widgets/class-info-text-card.php'); // require_ince(__DIR__. '/widgets/class-calltoaction.php'); // require_ince(__DIR__. '/widgets/class-textimonial.php'); } } B2W_Elementor_Extension::instance(); ?>

and another file class-buttons.php with

<?php /** * B2W Button Widget */ class B2w_Buttons_Widget extends \Elementor\Widget_Base { public function get_name() { return 'b2w_buttons'; } public function get_title() { return __('Button', 'plugins-b2w' ); } public function get_icon() { return 'eicon-button'; } public function get_keywords() { return [ 'b2w', 'button', 'link', 'ui', 'cta', 'happy' ]; } public function get_categories() { return ['b2w_category']; } public function _register_controls() { $this->start_controlls_section( 'b2w-buttons', [ 'label' => __('Button', 'plugin-b2w'), 'tab' => \Elementor\Controls_Manager::TAB_CONTENT, ] ); //add our controls $this->add-control ( 'button-text', [ 'label' => __('Button Text', 'plugin-b2w'), 'label_block' => true, 'placeholder' => __('Type something special here, my friend!', 'plugin-b2w'), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => __('Learn More', 'plugin-b2w'), ], ); $this->add-control ( 'button-link', [ 'label' => __('Button Link', 'plugin-b2w'), 'type' => \Elementor\Controls_Manager::URL, 'show_external' => true, 'default' => [ 'url' => '#', 'is_external' => true, 'nofollow' => false, ], ], ); $this->add-control ( 'button-style', [ 'label' => __('Button Style', 'plugin-b2w'), 'type' => \Elementor\Controls_Manager::SELECT, 'show_external' => true, 'default' => 'btn-primary', 'options' => [ 'btn-primary' => __('Primary', 'plugin-b2w'), 'btn-secondary' => __('secondary', 'plugin-b2w'), 'btn-invert' => __('invert', 'plugin-b2w'), ], ], ); $this->add_control ( 'button_align', [ 'label' => __('Alignment', 'plugin-b2w'), 'type' => \Elementor\Controls_Manager::CHOOSE, 'options' => [ 'text-start' => [ 'title' => __('left', 'plugin-b2w'), 'icon' => 'iecon-text-align-left', ], 'text-center' => [ 'title' => __('center', 'plugin-b2w'), 'icon' => 'iecon-text-align-center', ], 'text-end' => [ 'title' => __('right', 'plugin-b2w'), 'icon' => 'iecon-text-align-right', ], ], 'default' => 'text-start', 'toggle' => true, ], ); $this->end_controls_section(); } public function render(){ $settings = $this -> get_settings_for_display(); $target = $settings['button_link']['is_external'] ? 'target = "blank"' : ''; $nofollow = $settings['button_link']['nofollow'] ? 'rel="nofollow"' : ''; echo '
'; echo '' . $settings['button_text'] . ''; echo '
'; } } //Register Widget \Elementor\Plugin::instance()->widgets_manager->register_widget_type(new \B2w_Buttons_Widget()); ?>

and i don’t know what’s wrong, they say with these two files i can create a widget in local wordpress.

Hello there,

Have you tried adding those pieces of code to your functions.php file?

There is a helpful WP Beginner article about creating a custom widget that might be helpful!

Let me know if that helps!

Sam :woman_technologist:t3:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.