Halloween Sale 15% off Ends in See the plans
PHP

Custom Elementor Widget: Notice Box

Adds a simple "Notice Box" widget to Elementor with a title, text and four styles (info, success, warning, error), in its own "Site Widgets" panel category.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'elementor/elements/categories_registered', function ( $elements_manager ) {
    $elements_manager->add_category( 'wpide-site', array(
        'title' => 'Site Widgets',
        'icon'  => 'eicon-plug',
    ) );
} );

add_action( 'elementor/widgets/register', function ( $widgets_manager ) {
    if ( ! class_exists( '\Elementor\Widget_Base' ) ) {
        return;
    }

    if ( ! class_exists( 'WPIDE_Elementor_Notice_Widget' ) ) {
        class WPIDE_Elementor_Notice_Widget extends \Elementor\Widget_Base {
            public function get_name() {
                return 'wpide-notice';
            }

            public function get_title() {
                return 'Notice Box';
            }

            public function get_icon() {
                return 'eicon-alert';
            }

            public function get_categories() {
                return array( 'wpide-site' );
            }

            protected function register_controls() {
                $this->start_controls_section( 'content', array( 'label' => 'Notice' ) );
                $this->add_control( 'type', array(
                    'label'   => 'Style',
                    'type'    => \Elementor\Controls_Manager::SELECT,
                    'default' => 'info',
                    'options' => array( 'info' => 'Info', 'success' => 'Success', 'warning' => 'Warning', 'error' => 'Error' ),
                ) );
                $this->add_control( 'title', array( 'label' => 'Title', 'type' => \Elementor\Controls_Manager::TEXT, 'default' => 'Good to know' ) );
                $this->add_control( 'text', array( 'label' => 'Text', 'type' => \Elementor\Controls_Manager::TEXTAREA, 'default' => 'Write your message here.' ) );
                $this->end_controls_section();
            }

            protected function render() {
                $s      = $this->get_settings_for_display();
                $colors = array( 'info' => '#2271b1', 'success' => '#00a32a', 'warning' => '#dba617', 'error' => '#d63638' );
                $color  = isset( $colors[ $s['type'] ] ) ? $colors[ $s['type'] ] : $colors['info'];

                printf(
                    '<div class="wpide-notice wpide-notice--%1$s" role="note" style="border-left:4px solid %2$s;background:color-mix(in srgb,%2$s 8%%,transparent);padding:1em 1.25em;border-radius:4px;"><strong style="display:block;margin-bottom:.25em;">%3$s</strong>%4$s</div>',
                    esc_attr( $s['type'] ),
                    esc_attr( $color ),
                    esc_html( $s['title'] ),
                    wp_kses_post( wpautop( $s['text'] ) )
                );
            }
        }
    }

    $widgets_manager->register( new WPIDE_Elementor_Notice_Widget() );
} );

Notes

Use this as a starting point for your own widgets: add controls in register_controls() and print them in render().

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.