Skip to content

Instantly share code, notes, and snippets.

@DaveyJake
Created September 5, 2019 06:26
Show Gist options
  • Select an option

  • Save DaveyJake/53ebfb0c5b7bcb2ee7d7a3bc6cb4cdd4 to your computer and use it in GitHub Desktop.

Select an option

Save DaveyJake/53ebfb0c5b7bcb2ee7d7a3bc6cb4cdd4 to your computer and use it in GitHub Desktop.
Basic WP Widget Template
<?php
/**
* This is just a widget template file. Be sure to change everything your
* reading to suit your website's needs.
*
* IF YOU'RE A WORDPRESS NEWB AND DO NOT KNOW WHAT YOU'RE DOING...
*
* This entire file, with the exception of the lines that contain
* `public [static] function` can be modified to whatever you wish. Just be sure
* to NOT rename any of the methods contained in this class. For more information,
* please visit {@link https://codex.wordpress.org/Widget_API#Developing_Widgets}.
*
* @author Davey Jacobson <djacobson@usa.rugby>
* @version 0.0.1a
* @package Davey_Jacobson
* @subpackage Widget_Template
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if directly accessed.
// This action hook is what registers the widget when the website initialized.
add_action( 'widgets_init', array( 'DJ_Widget_Template', 'register' ) );
// This is where our actual widget begins.
class DJ_Widget_Template extends WP_Widget {
/**
* The primary widget constructor.
*
* @return DJ_Widget_Template
*/
public function __construct() {
// Change this to suit your needs.
$id_base = 'dj_widget_template';
// Change this to suit your needs.
$name = __( 'Widget Template', 'daveyjacobson-widget-template' );
/**
* If viewing the new {@link https://developer.wordpress.org/reference/functions/wp_register_sidebar_widget/}
* documentation, this is the `$options` parameter found in {@see 'wp_register_sidebar_widget'}.
*
* For information on the `customize_selective_refresh` option, read
* {@link https://wp.me/p2AvED-4rg} which explains how to implement
* selective refresh support for widgets.
*
* @var array
*/
$widget_options = array(
'classname' => 'widget_' . $id_base,
'description' => __( 'Widget description goes here.', 'daveyjacobson-widget-template' ),
'customize_selective_refresh' => false,
);
/**
* The full description for widget control options can be found at
* {@link https://codex.wordpress.org/Function_Reference/wp_register_widget_control#Description}.
*
* If viewing the new {@link https://developer.wordpress.org/reference/functions/wp_register_widget_control/#parameters}
* docs, this is the `$options` parameter in {@see 'wp_register_widget_control'}.
*
* @var array
*/
$control_options = array(
'height' => 200, // This option is never used. Default: 200.
'width' => 250, // The width of the fully expanded control form--TRY HARD to use the default width (e.g. 250).
'id_base' => $id_base, // This option is meant for widgets that allow multiple instances of itself.
);
/**
* This is the literal {@see 'WP_Widget::__construct'} method.
*
* @param string $id_base Optional Base ID for the widget, lowercase and unique. If left empty,
* a portion of the widget's class name will be used Has to be unique.
* @param string $name Name for the widget displayed on the configuration page.
* @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for information
* on accepted arguments. Default empty array.
* @param array $control_options Optional. Widget control options. See wp_register_widget_control() for
* information on accepted arguments. Default empty array.
*/
parent::__construct(
$id_base,
$name,
$widget_options,
$control_options
);
}
/**
* The actual widget instance (e.g. content). The `$args` are located in
* {@see 'DJ_Widget_Template::form'}.
*
* @see WP_Widget::widget()
*
* @param array $args Default widget arguments.
* @param mixed $instance The individual widget's instance.
*/
public function widget( $args, $instance ) {
// Our example variables from the widget form.
$title = apply_filters( 'widget_title', $instance['title'] );
$name = apply_filters( 'widget_name', $instance['name'] );
$show_info = apply_filters( 'widget_show_info', $instance['show_info'] );
echo $args['before_widget'];
/*
* This is where you'd include your primary widget content file with
* the template of how your widget is to be viewed on the frontend.
*
* Uncomment below if and replace 'widget-template-file.php'
* with your own template file.
*/
#include 'widget-template-file.php';
echo $args['after_widget'];
}
/**
* Update the widget by setting the current `$instance` to the `$old_instance`.
*
* @see WP_Widget::update()
*
* @param mixed $new_instance The new data for the widget.
* @param mixed $old_instance The old data to be replaced.
*
* @return mixed The updated widget instance.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
// Strip tags from title and name to remove HTML
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['name'] = strip_tags( $new_instance['name'] );
$instance['show_info'] = $new_instance['show_info'];
return $instance;
}
/**
* This generates the mini-form you see on `Appearances > Widgets` area of
* the dashboard. Change the `$args` below to suit your needs.
*
* REMEMBER: The values shown below are EXAMPLE values for this widget
* template. Be sure to change them to suit your needs.
*
* @see WP_Widget::form()
*
* @param array $args {
* These are the previously saved values from the website database.
*
* @type string $title The widget title.
* @type string $name The widget name.
* @type bool $show_info Will this widget show the info?
* }
*/
public function form( $args ) {
// Set up some default widget settings.
$defaults = array(
'title' => __( 'Widget Title Goes Here', 'daveyjacobson-widget-template' ),
'name' => __( 'Widget Name Goes Here', 'daveyjacobson-widget-template' ),
'show_info' => true
);
$instance = wp_parse_args( $args, $defaults );
// Example Widget Title: Text input field.
$title = '<p>' .
'<label for="' . $this->get_field_id( 'title' ) . '">' .
_e( 'Title:', 'daveyjacobson-widget-template' ) .
'</label>' .
'<input id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '"' .
'value="' . $instance['title'] . '" style="width:100%;" />' .
'</p>';
// Example Widget Name: Also a text input field.
$name = '<p>' .
'<label for="' . $this->get_field_id( 'name' ) . '">' .
_e( 'Name:', 'daveyjacobson-widget-template' ) .
'</label>' .
'<input id="' . $this->get_field_id( 'name' ) . '" name="' . $this->get_field_name( 'name' ) . '"' .
'value="' . $instance['name'] . '" style="width:100%;" />' .
'</p>';
// Example Widget Info: This is a checkbox input field.
$show_info = '<p>' .
'<label for="' . $this->get_field_id( 'show_info' ) . '">' .
_e( 'Show Info?', 'daveyjacobson-widget-template' ) .
'</label>' .
'<input id="' . $this->get_field_id( 'show_info' ) . '" name="' . $this->get_field_name( 'show_info' ) . '"' .
'type="checkbox" value="' . $instance['show_info'] . '" ' . ( $instance['show_info'] ? 'checked ' : '' ) . '/>' .
'</p>';
echo $title . $name . $show_info;
}
/**
* Might as well ensure the widget can register itself when ready.
*
* @since DJ_Widget_Template 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