Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
WordPress: Custom widget
Plugin Name: Messenger Widget
Plugin URI:
Description: Display any messages.
Version: 1.0
Author: Vien Hoang
Author URI:
License: GPL2
class Messenger extends WP_Widget {
public function __construct() {
// Description and name for the widget on dashboard
$params = array(
'description' => 'Display messages to readers',
'name' => 'Messenger'
parent::__construct( 'Messenger', '', $params );
public function form( $instance ) {
extract( $instance );
<label for="<?php echo $this->get_field_id( 'title' );?>">Title: </label>
id="<?php echo $this->get_field_id( 'title' );?>"
name="<?php echo $this->get_field_name( 'title' );?>"
value="<?php if ( isset( $title ) ) echo esc_attr( $title );?>"
<label for="<?php echo $this->get_field_id( 'description' );?>">Description: </label>
id="<?php echo $this->get_field_id( 'description' );?>"
name="<?php echo $this->get_field_name( 'description' );?>"><?php if ( isset( $description ) ) echo esc_attr( $description );?>
// Method for responsible for displaying
public function widget( $args, $instance ) {
extract( $args );
extract( $instance );
$title = apply_filters( 'widget_title', $title );
$description = apply_filters( 'widget_description', $description );
echo $before_widget;
echo $before_title . $title . $after_title;
echo "<p>{$description}</p>";
echo $after_widget;
add_action( 'widgets_init', 'vh_register_messenger' );
function vh_register_messenger() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.