Skip to content

Instantly share code, notes, and snippets.

@jeremyworboys
Created March 16, 2012 12:45
Show Gist options
  • Save jeremyworboys/2049932 to your computer and use it in GitHub Desktop.
Save jeremyworboys/2049932 to your computer and use it in GitHub Desktop.
Wordpress: Start Widget
<?php
error_reporting(E_ALL);
/*
Plugin Name: PLUGIN_NAME
Plugin URI: PLUGIN_URI
Description: PLUGIN_DESCRIPTION
Version: 1.0
Author: Jeremy Worboys
Author URI: http://complexcompulsions.com
*/
class PLUGIN_CLASS extends WP_Widget {
//--------------------------------------------------------------------------
/**
* Runs when the class is instantiated.
*/
function __construct()
{
$params = array(
'name' => 'PLUGIN_NAME',
'description' => 'PLUGIN_DESCRIPTION'
);
parent::__construct('PLUGIN_CLASS', '', $params);
}
//--------------------------------------------------------------------------
/**
* Displays the settings form on the widget configuration page.
*/
public function form($instance)
{
extract($instance);
?>
<p>
<label for="<?php echo $this->get_field_id('title') ?>">Title: </label>
<input type="text" class="widefat"
id="<?php echo $this->get_field_id('title') ?>"
name="<?php echo $this->get_field_name('title') ?>"
value="<?php echo (isset($title)) ? esc_attr($title) : '' ?>">
</p>
<?php
}
//--------------------------------------------------------------------------
/**
* Displays the widget on the front-end of the website.
*/
public function widget($args, $instance)
{
extract($args);
extract($instance);
// Provide default values
$title = (!empty($title)) ? $title : 'Recent Tweets';
$data = $this->tweets($username, $tweet_count);
// Check $data came back ok
if ($data === false || !isset($data->tweets)) {
return false;
}
// Apply filters
$title = apply_filters('widget_title', $title);
// Output widget
echo $before_widget;
echo $before_title . $title . $after_title;
echo WIDGET_CONTENTS;
echo $after_widget;
}
//==========================================================================
PRIVATE_FUNCTIONS
//--------------------------------------------------------------------------
}
add_action('widgets_init', 'register_PLUGIN_CLASS');
function register_PLUGIN_CLASS()
{
register_widget('PLUGIN_CLASS');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment