Skip to content

Instantly share code, notes, and snippets.

@mhulse
Last active December 18, 2015 23:49
Show Gist options
  • Save mhulse/5864117 to your computer and use it in GitHub Desktop.
Save mhulse/5864117 to your computer and use it in GitHub Desktop.
My personal WordPress sidebar widget setup using PHP5 OOP
<?php
# Custom widgets:
include_once('classes/THEMENAME/widgets/widgets.php');
<?php
include_once('class.foo_widget_1.php');
// ... add more custom widgets here ...
//include_once('class.foo_widget_2.php');
// ... and so on ...
// ... :) ...
<?php
include_once('class.foo_widget_setup.php');
include_once('class.foo_base_widget_1.php');
/**
* This is the basic widget boilerplate.
*/
class Foo_Widget_1 extends Foo_Base_Widget_1
{
public function __construct() {
$this->template = array('widgets/foo-widget-1.php',);
parent::__construct(
'foo_widget_1',
'Foo Widget 1',
array(
'classname' => __CLASS__,
'description' => 'Foo Widget 1\'s description ...',
)
);
new Foo_Widget_Setup(
__CLASS__
);
}
}
new Foo_Widget_1();
<?php
/**
* @see http://www.jonloomer.com/2012/06/28/create-custom-wordpress-widget/
* @see http://wordpress.stackexchange.com/a/80433/32387
* @see http://wordpress.stackexchange.com/a/45920/32387
* @see http://wordpress.stackexchange.com/a/48094/32387
*/
class Foo_Widget_Setup
{
private $widget_class = '';
private $style_admin = array();
private $script_admin = array();
private $style = array();
private $script = array();
private $style_defaults = array(
'handle' => '',
'src' => '',
'deps' => array(),
'version' => false,
'media' => 'all',
);
private $script_defaults = array(
'handle' => '',
'src' => '',
'deps' => array(),
'version' => false,
'in_footer' => false,
);
public function __construct(
$widget_class = '',
$style_admin = array(),
$script_admin = array(),
$style = array(),
$script = array()
) {
if ($style_admin) {
$this->style_admin = $style_admin;
add_action('admin_enqueue_scripts', array($this, 'add_styles'));
}
if ($script_admin) {
$this->script_admin = $script_admin;
add_action('admin_enqueue_scripts', array($this, 'add_scripts'));
}
if ($style) {
$this->style = $style;
add_action('wp_enqueue_scripts', array($this, 'add_styles'));
}
if ($script) {
$this->script = $script;
add_action('wp_enqueue_scripts', array($this, 'add_styles'));
}
if ( ! empty($widget_class)) {
$this->widget_class = $widget_class;
add_action(
'widgets_init',
array(
$this,
'register_widget',
)
);
}
}
public function register_widget() {
register_widget($this->widget_class);
}
public function add_styles() {
return $this->add_files('style');
}
public function add_scripts() {
return $this->add_files('script');
}
private function add_files($kind = '') {
$return = FALSE;
if ($kind) {
$files = (is_admin()) ? $this->{ $kind . '_admin' } : $this->{ $kind };
if ( ! empty($files)) {
foreach($files as $file) {
$args = wp_parse_args($file, $this->{ $kind . '_defaults' });
call_user_func_array(
('wp_enqueue_' . $kind),
array(
$args['handle'],
$args['src'],
$args['deps'],
$args['version'],
((array_key_exists('media', $args)) ? $args['media'] : $args['in_footer']),
)
);
}
$return = TRUE;
}
}
return $return;
}
}
<?php
/**
* Generic sidebar widget that contains a title and text field.
*
* See "Text widget class" in `/wp-includes/default-widgets.php`.
*
* @todo Option to make this a single-instance widget.
* @todo Need constant that allows user to pick template base location.
* @todo Make more like WPAlchemy.
*
* @see http://wordpress.stackexchange.com/questions/32103
* @see http://wordpress.stackexchange.com/a/1834/32387
* @see http://wordpress.stackexchange.com/questions/104077
* @see https://github.com/farinspace/wpalchemy
*/
abstract class Foo_Base_Widget_1 extends WP_Widget
{
protected $template = array();
public function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['text'] = $new_instance['text']; // Unfiltered HTML.
return $instance;
}
public function form($instance) {
$instance = wp_parse_args((array) $instance, array('title' => '', 'text' => '',));
$title = strip_tags($instance['title']);
$text = esc_textarea($instance['text']);
?>
<p>
<label for="<?=$this->get_field_id('title')?>">Title:</label>
<input type="text" name="<?=$this->get_field_name('title')?>" id="<?=$this->get_field_id('title')?>" class="widefat" value="<?=esc_attr($title)?>">
</p>
<textarea rows="16" cols="20" name="<?=$this->get_field_name('text')?>" id="<?=$this->get_field_id('text')?>" class="widefat"><?=$text?></textarea>
<?php
}
/**
* @see http://wordpress.stackexchange.com/a/4471/32387
*/
public function widget($args, $instance) {
extract($args);
$title = apply_filters(
'widget_title', // The name (`$tag`) of the filter hook.
(empty($instance['title']) ? '' : $instance['title']), // The value which the filters hooked to `$tag` may modify.
$instance, // Additional variable passed to the filter functions.
$this->id_base // IBID.
);
$text = apply_filters(
'widget_text',
(empty($instance['text']) ? '' : $instance['text']),
$instance
);
if ($text != '') {
echo $text;
} else {
# Remember: Arguments will be available to included template.
if ( ! empty($this->template)) include(locate_template($this->template));
}
}
}
Within each widget's template file, I have access to the <?=$title?> variable.
If someone has filled-in the "text" field, then that will trump the template and the value of $text will be echoed.
Here's an example template where I use the $title to get Twitter username; if that does not exist, I get an option from the theme's setting page.
<?php
$twitter = '';
if ($title) {
/**
* Use the "title" of the widget as the Twitter username.
*/
$twitter = $title;
} else {
/**
* Get the Twitter username from the theme options.
*/
$options = get_option('THEMENAME_options');
if (is_array($options) && $options['username']) {
$user = get_user_by('login', $options['username']);
if ($user) $twitter = get_the_author_meta('twitter', $user->ID);
}
}
?>
<?php if ($twitter): ?>
<a
class="twitter-timeline"
href="https://twitter.com/<?=$twitter?>"
data-widget-id="347149624567558145"
data-screen-name="<?=$twitter?>"
width="100%"
>
Tweets by @<?=$twitter?>
</a>
<?php endif; ?>
@mhulse
Copy link
Author

mhulse commented Jun 26, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment