Created
September 20, 2023 14:52
-
-
Save damiencarbery/a4b6e2347f1dbc23d1e8c833bed3e969 to your computer and use it in GitHub Desktop.
Elementor Days Widget - Display different text based on the day of the week. https://www.damiencarbery.com/2023/10/custom-elementor-widget-daily-text/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly. | |
} | |
// Elementor DaysMessages Widget - Elementor widget that displays different text based on the day of the week. | |
class Elementor_DaysMessages_Widget extends \Elementor\Widget_Base { | |
// Set widget internal name. | |
public function get_name() { | |
return 'daysmessages'; | |
} | |
// Set widget title. | |
public function get_title() { | |
//return esc_html__( 'oEmbed', 'elementor-oembed-widget' ); | |
return 'Days Messages'; | |
} | |
// Set widget icon - use a calendar for this widget. | |
public function get_icon() { | |
return 'eicon-calendar'; | |
} | |
// Set custom help URL - Set a URL where the user can get more information about the widget. | |
// public function get_custom_help_url() { | |
// return 'https://developers.elementor.com/docs/widgets/'; | |
// } | |
// Set widget categories - Set the list of categories the widget belongs to. | |
public function get_categories() { | |
return [ 'general' ]; | |
} | |
// Set widget keywords - set the list of keywords the widget belongs to. | |
public function get_keywords() { | |
return [ 'days', 'messages' ]; | |
} | |
// Register widget controls - input fields to allow the user to customize the widget settings. | |
protected function register_controls() { | |
$this->start_controls_section( | |
'content_section', | |
[ | |
'label' => esc_html( 'Content' ), | |
'tab' => \Elementor\Controls_Manager::TAB_CONTENT, | |
] | |
); | |
$this->add_control( | |
'monday', | |
[ | |
'label' => esc_html( 'Monday text' ), | |
'type' => \Elementor\Controls_Manager::TEXT, | |
'placeholder' => esc_html( 'Monday text' ), | |
'classes' => 'monday-text', | |
] | |
); | |
$this->add_control( | |
'tuesday', | |
[ | |
'label' => esc_html( 'Tuesday text' ), | |
'type' => \Elementor\Controls_Manager::TEXT, | |
'placeholder' => esc_html( 'Tuesday text' ), | |
'classes' => 'tuesday-text', | |
] | |
); | |
$this->add_control( | |
'wednesday', | |
[ | |
'label' => esc_html( 'Wednesday text' ), | |
'type' => \Elementor\Controls_Manager::TEXT, | |
'placeholder' => esc_html( 'Wednesday text' ), | |
'classes' => 'wednesday-text', | |
] | |
); | |
$this->add_control( | |
'thursday', | |
[ | |
'label' => esc_html( 'Thursday text' ), | |
'type' => \Elementor\Controls_Manager::TEXT, | |
'placeholder' => esc_html( 'Thursday text' ), | |
'classes' => 'thursday-text', | |
] | |
); | |
$this->add_control( | |
'friday', | |
[ | |
'label' => esc_html( 'Friday text' ), | |
'type' => \Elementor\Controls_Manager::TEXT, | |
'placeholder' => esc_html( 'Friday text' ), | |
'classes' => 'friday-text', | |
] | |
); | |
$this->add_control( | |
'saturday', | |
[ | |
'label' => esc_html( 'Saturday text' ), | |
'type' => \Elementor\Controls_Manager::TEXT, | |
'placeholder' => esc_html( 'Saturday text' ), | |
'classes' => 'saturday-text', | |
] | |
); | |
$this->add_control( | |
'sunday', | |
[ | |
'label' => esc_html( 'Sunday text' ), | |
'type' => \Elementor\Controls_Manager::TEXT, | |
'placeholder' => esc_html( 'Sunday text' ), | |
'classes' => 'sunday-text', | |
] | |
); | |
$this->end_controls_section(); | |
$this->start_controls_section( | |
'style_section', | |
[ | |
'label' => esc_html( 'Style' ), | |
'tab' => \Elementor\Controls_Manager::TAB_STYLE, | |
] | |
); | |
$this->add_control( | |
'text_color', | |
[ | |
'label' => esc_html( 'Text Color' ), | |
'type' => \Elementor\Controls_Manager::COLOR, | |
'selectors' => [ | |
'{{WRAPPER}}' => 'color: {{VALUE}}', | |
], | |
] | |
); | |
$this->add_group_control( | |
\Elementor\Group_Control_Typography::get_type(), | |
[ | |
'name' => 'content_typography', | |
'selector' => '{{WRAPPER}}', | |
] | |
); | |
$this->add_group_control( | |
\Elementor\Group_Control_Text_Stroke::get_type(), | |
[ | |
'name' => 'text_stroke', | |
'selector' => '{{WRAPPER}}', | |
] | |
); | |
$this->add_group_control( | |
\Elementor\Group_Control_Text_Shadow::get_type(), | |
[ | |
'name' => 'text_shadow', | |
'selector' => '{{WRAPPER}}', | |
] | |
); | |
$this->end_controls_section(); | |
} | |
// Render the DaysMessages widget on the frontend. | |
protected function render() { | |
$settings = $this->get_settings_for_display(); | |
$date = getdate(); | |
$days = array( 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' ); | |
$day_text = $settings[ $days[ $date[ 'wday' ] ] ]; | |
if ( is_admin() ) { | |
$day_text .= ' (<em>Showing ' . ucfirst( $days[ $date[ 'wday' ] ] ) . ' text)</em>'; | |
} | |
if ( ! empty( $day_text ) ) { | |
echo '<div class="days-messages-elementor-widget">'; | |
echo $day_text; | |
echo '</div>'; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Elementor Days Widget | |
* Description: Display different text based on the day of the week. | |
* Plugin URI: https://www.damiencarbery.com/2023/10/custom-elementor-widget-daily-text/ | |
* Version: 0.1 | |
* Author: Damien Carbery | |
* Author URI: https://www.damiencarbery.com/ | |
* | |
* Elementor tested up to: 3.16.3 | |
*/ | |
// This is a demo widget to experiment with the Elementor API. | |
// API docs are at: https://developers.elementor.com/docs/widgets/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly. | |
} | |
// Register Days Widget. | |
function dcwd_register_days_widget( $widgets_manager ) { | |
require_once( __DIR__ . '/days-messages-widget.php' ); | |
$widgets_manager->register( new \Elementor_DaysMessages_Widget() ); | |
} | |
add_action( 'elementor/widgets/register', 'dcwd_register_days_widget' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment