Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress: Custom widget
<?php
/*
Plugin Name: Messenger Widget
Plugin URI: http://vienhoang.com
Description: Display any messages.
Version: 1.0
Author: Vien Hoang
Author URI: http://vienhoang.com
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 );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' );?>">Title: </label>
<input
class="widefat"
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 );?>"
/>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'description' );?>">Description: </label>
<textarea
class="widefat"
id="<?php echo $this->get_field_id( 'description' );?>"
rows="5"
name="<?php echo $this->get_field_name( 'description' );?>"><?php if ( isset( $description ) ) echo esc_attr( $description );?>
</textarea>
</p>
<?php
}
// 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() {
register_widget('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.