Skip to content

Instantly share code, notes, and snippets.

@drblue
Last active June 17, 2021 08:00
Show Gist options
  • Save drblue/9d1a6763a9f773ad7b87ac1eb7ec8cd9 to your computer and use it in GitHub Desktop.
Save drblue/9d1a6763a9f773ad7b87ac1eb7ec8cd9 to your computer and use it in GitHub Desktop.
A skeleton of a widget.
<?php
class MyWidgetSkeleton extends WP_Widget {
/**
* Construct a new widget instance.
*/
public function __construct() {
parent::__construct(
'my-widget-skeleton', // Base ID
'Skeleton Widget 💀', // Name
[
'description' => 'A widget skeleton.',
]
);
}
/**
* Front-end display of the widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved option values for this specific instance of the widget.
* @return void
*/
public function widget($args, $instance) {
// start widget
echo $args['before_widget'];
// end widget
echo $args['after_widget'];
}
/**
* Back-end widget form
*
* @see WP_Widget::form()
*
* @param array $instance Current saved values for this instance of the widget.
* @return void
*/
public function form($instance) {
// do we have a title set? if so, use it, otherwise set empty title
$title = isset($instance['title'])
? $instance['title']
: __('A skeleton title', 'textdomain');
?>
<!-- title -->
<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') ?>"
type="text"
value="<?php echo $title; ?>"
>
</p>
<?php
}
/**
* Sanitize widget form data before they are saved to the database.
*
* @see WP_Widget::update()
*
* @param array $new_instance Form values just sent to be saved.
* @param array $old_instance Currently saved values.
* @return void
*/
public function update($new_instance, $old_instance) {
$instance = [];
$instance['title'] = (!empty($new_instance['title']))
? strip_tags($new_instance['title'])
: '';
return $instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment