Create a Custom Widget
Create a simple custom widget that displays a message or any other content.
<?php
class Custom_Message_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'custom_message_widget', // Base ID
__('Custom Message Widget', 'text_domain'), // Name
array('description' => __('A Custom Widget that displays a message.', 'text_domain'),) // Args
);
}
public function widget($args, $instance) {
$title = !empty($instance['title']) ? $instance['title'] : __('New title', 'text_domain');
$message = !empty($instance['message']) ? $instance['message'] : __('Your custom message here', 'text_domain');
echo $args['before_widget'];
if (!empty($title)) {
echo $args['before_title'] . $title . $args['after_title'];
}
echo '<p>' . $message . '</p>';
echo $args['after_widget'];
}
public function form($instance) {
$title = !empty($instance['title']) ? $instance['title'] : __('New title', 'text_domain');
$message = !empty($instance['message']) ? $instance['message'] : __('Your custom message here', 'text_domain');
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('message'); ?>"><?php _e('Message:'); ?></label>
<textarea class="widefat" id="<?php echo $this->get_field_id('message'); ?>" name="<?php echo $this->get_field_name('message'); ?>" rows="5"><?php echo esc_textarea($message); ?></textarea>
</p>
<?php
}
public function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
$instance['message'] = (!empty($new_instance['message'])) ? strip_tags($new_instance['message']) : '';
return $instance;
}
}
function register_custom_message_widget() {
register_widget('Custom_Message_Widget');
}
add_action('widgets_init', 'register_custom_message_widget');
Post Comment