Skip to content

Instantly share code, notes, and snippets.

@rosswintle
Created May 19, 2020 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosswintle/831903464d403f9d29fcfd044af60344 to your computer and use it in GitHub Desktop.
Save rosswintle/831903464d403f9d29fcfd044af60344 to your computer and use it in GitHub Desktop.
Simple WordPress Widget for Jack McDade's Weird Wide Webring
<?php
/**
* Plugin Name: Weird Wide Webring
* Plugin URI: https://weirdwidewebring.net
* Description: A widget for displaying the Weird Wide Webring links
* Author: Ross Wintle
* Author URI: https://rosswintle.uk/
* Text Domain: weird-wide-webring
* Domain Path: /languages
* Version: 1.0.0
*
* @package Weird_Wide_Webring
*/
class Weird_Wide_Webring_Widget extends WP_Widget {
public $links;
/**
* Sets up the widgets name etc
*/
public function __construct() {
$this->links = [
'Previous site' => 'https://weirdwidewebring.net/prev.html?ref=' . esc_attr(home_url()),
'Next site' => 'https://weirdwidewebring.net/next.html?ref=' . esc_attr(home_url()),
'Random site' => 'https://weirdwidewebring.net/random.html',
'List all sites' => 'https://weirdwidewebring.net',
'Join the ring' => 'https://weirdwidewebring.net/join.html'
];
$widget_ops = array(
'title' => 'Weird Wide Webring',
'description' => '',
);
parent::__construct( 'weird_wide_webring_widget', 'Weird Wide Webring', $widget_ops );
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
if ( ! empty( $instance['description'] ) ) {
printf('<p>%s</p>', esc_html($instance['description']));
}
echo '<ul>';
foreach (apply_filters('weird_wide_webring_links', $this->links) as $label => $link) {
printf('<li><a href="%s">%s</a></li>', esc_url($link), esc_html($label));
}
echo '</ul>';
echo $args['after_widget'];
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
$description = ! empty( $instance['description'] ) ? $instance['description'] : '';
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'weird-wide-webring' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'description' ) ); ?>"><?php esc_attr_e( 'Description:', 'weird-wide-webring' ); ?></label>
<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'description' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'description' ) ); ?>"><?php echo esc_html( $description ); ?></textarea>
</p>
<?php
}
/**
* Processing widget options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*
* @return array
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
$instance['description'] = ( ! empty( $new_instance['description'] ) ) ? sanitize_text_field( $new_instance['description'] ) : '';
return $instance;
}
}
add_action( 'widgets_init', function(){
register_widget( 'Weird_Wide_Webring_Widget' );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment