Skip to content

Instantly share code, notes, and snippets.

@DaveyJake
Last active February 24, 2021 10:11
Show Gist options
  • Save DaveyJake/379620bffeebebf62838902a5c292ebd to your computer and use it in GitHub Desktop.
Save DaveyJake/379620bffeebebf62838902a5c292ebd to your computer and use it in GitHub Desktop.
[WP] Widget Skeleton
<?php
/**
* Widgets API: Widget Skeleton
*
* A generic skeleton template for widgets.
*
* @package Davey_Jacobson
* @subpackage Widget_Skeleton
*/
defined( 'ABSPATH' ) || exit;
// Uncomment to activate the widget.
//add_action( 'widgets_init', array( 'DJ_Widget_Skeleton', 'register' ), 20 );
/**
* Widget skeleton.
*
* @since 1.0.0
*/
class DJ_Widget_Skeleton extends WP_Widget {
/**
* Theme namespace. Use if widget is part of a theme.
*
* @since 1.0.0
* @var string
*/
public $namespace;
/**
* Primary constructor.
*
* @since DJ 1.0.0
*/
public function __construct() {
/**
* The widget namespace if part of a theme.
*
* @var string
*/
$this->namespace = '';
/**
* Optional Base ID for the widget, lowercase and unique.
*
* @var string
*/
$this->id_base = '';
/**
* Name for the widget displayed on the configuration page.
*
* @var string
*/
$this->name = '';
/**
* Widget options. See wp_register_sidebar_widget() for information on accepted arguments.
* Optional. Default empty array.
*
* @var array
*/
$this->widget_options = array();
/**
* Widget control options. See {@see 'wp_register_widget_control'} for information on accepted
* arguments. Optional. Default empty array.
*
* @var array
*/
$this->control_options = array();
//
// Additional widget details go here.
//
}
/**
* Actual widget content and output.
*
* @since DJ 1.0.0
*
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance The settings for the particular instance of the widget.
*/
public function widget( $args, $instance ) {
// Widget output in the front end.
}
/**
* Widget updater.
*
* @since DJ 1.0.0
*
* @param array $new_instance New settings for this instance as input by the user via
* WP_Widget::form().
* @param array $old_instance Old settings for this instance.
*
* @return array Settings to save or bool false to cancel saving.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
foreach ( $new_instance as $key => $value ) {
$hook = ( ! empty( $this->namespace ) ? "{$this->namespace}_" : '' ) . "{$this->option_name}_{$key}";
if ( ! empty( $value ) ) {
$instance[ $key ] = apply_filters( $hook, $value );
}
}
return $instance;
}
/**
* The backend widget form.
*
* @since DJ 1.0.0
*
* @param array $instance The current widget fields.
*/
public function form( $instance ) {
// Backend Form.
}
/**
* Register this widget.
*
* @static
*
* @since DJ 1.0.0
*/
public static function register() {
register_widget( __CLASS__ );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment