Skip to content

Instantly share code, notes, and snippets.

@drblue
Created May 22, 2019 13:02
Show Gist options
  • Save drblue/ada8d8fb41475e1617a7e779e2004c44 to your computer and use it in GitHub Desktop.
Save drblue/ada8d8fb41475e1617a7e779e2004c44 to your computer and use it in GitHub Desktop.
Empty StarWars Widget
<?php
/**
* Adds StarWarsWidget widget.
*/
class StarWarsWidget extends WP_Widget {
/**
* Register widget with WordPress.
*/
public function __construct() {
parent::__construct(
'wcms18-starwars-widget', // Base ID
'WCMS18 StarWars', // Name
[
'description' => __('A Widget for displaying some StarWars trivia', 'wcms18-starwars-widget'),
] // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title']);
// start widget
echo $before_widget;
// title
if (! empty($title)) {
echo $before_title . $title . $after_title;
}
// content
echo "Here be some StarWars trivia.";
// close widget
echo $after_widget;
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form($instance) {
if (isset($instance['title'])) {
$title = $instance['title'];
} else {
$title = __('StarWars Trivia', 'wcms18-starwars-widget');
}
?>
<!-- title -->
<p>
<label
for="<?php echo $this->get_field_name('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>
<!-- /title -->
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update($new_instance, $old_instance) {
$instance = [];
$instance['title'] = (!empty($new_instance['title']))
? strip_tags($new_instance['title'])
: '';
return $instance;
}
} // class StarWarsWidget
<?php
/**
* Plugin Name: WCMS18 StarWars Widget
* Plugin URI: https://thehiveresistance.com/wcms18-starwars
* Description: This plugin for displaying some StarWars trivia.
* Version: 0.1
* Author: Johan Nordström
* Author URI: https://thehiveresistance.com
* License: WTFPL
* License URI: http://www.wtfpl.net/
* Text Domain: wcms18-starwars
* Domain Path: /languages
*/
require("class.StarWarsWidget.php");
function wsw_widgets_init() {
register_widget('StarWarsWidget');
}
add_action('widgets_init', 'wsw_widgets_init');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment