-
-
Save jhaus/716257 to your computer and use it in GitHub Desktop.
Abstract class to create easily wordpress widgets, documented here http://www.strx.it/2010/11/wordpress-widgets-template/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SimplestWidget_Widget extends Strx_Widget { | |
function w_id(){ return 'simplest-widget'; } | |
function w_name(){ return 'My Simplest Widget'; } | |
/** Return the dashboard admin form */ | |
function w_form($instance){ | |
return '<p>'.$this->w_form_input($instance, 'title').'</p>'; | |
} | |
/** Return the real widget content */ | |
function w_content($instance){ | |
return 'This is My first widget using Strx_Widget class'; | |
} | |
/** Widget Default Options, abstract */ | |
function w_defaults(){ | |
return array( | |
'title' => __($this->w_name()), | |
); | |
} | |
} | |
SimplestWidget_Widget::w_init(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** Wordpress Widget Helper Class */ | |
abstract class Strx_Widget extends WP_Widget { | |
/*********************************************************** | |
** Abstract Functions | |
***********************************************************/ | |
/** Return the pugin id, ie: my-plugin */ | |
abstract function w_id(); | |
/** Return the pugin name, ie: My Plugin */ | |
abstract function w_name(); | |
/** Return the form content to show in admin dashboard */ | |
abstract function w_form($instance); | |
/** Return the real widget content */ | |
abstract function w_content($instance); | |
/** Return the plugin default options, a name=>value array, ie: array('title'=>'My Plugin Title') */ | |
abstract function w_defaults(); | |
/** Title field to retrieve from options */ | |
function w_title_field(){ return 'title'; } | |
/*********************************************************** | |
** Static Functions | |
***********************************************************/ | |
/** Register the Widget using Wordpress Actions */ | |
static function w_init($classname=null){ | |
if (is_null($classname)){ | |
$classname=get_called_class(); | |
} | |
add_action( 'widgets_init', create_function('', 'return register_widget("'.$classname.'");') ); | |
} | |
/*********************************************************** | |
** Wordpress Hooks | |
***********************************************************/ | |
/** Class constructor */ | |
function Strx_Widget(){ | |
$widget_ops=$this->w_defaults(); | |
$control_ops = array('width' => 400); | |
$this->WP_Widget($this->w_id(), $this->w_name(), $widget_ops, $control_ops); | |
} | |
/** Admin Dashboard Form */ | |
function form($instance) { | |
$instance = $this->parse_args( $instance ); | |
echo $this->w_form($instance); | |
} | |
/** Widget Code */ | |
function widget($args, $instance) { | |
extract($args); | |
$tfld=$this->w_title_field(); | |
echo $before_widget; | |
if (!empty($tfld)){ | |
$title = apply_filters('widget_title', empty($instance['title']) ? ' ' : $instance['title'], $instance, $this->id_base); | |
if ( $title ){ echo $before_title . $title . $after_title; } | |
} | |
echo '<div class="'.$this->id_base.'-wrapper">'.$this->w_content($instance).'</div>'; | |
echo $after_widget; | |
} | |
/** Called By Wordpress when saving settings */ | |
function update($new_instance, $old_instance) { | |
$instance = $old_instance; | |
$new_instance = $this->parse_args( $new_instance ); | |
$def=$this->w_defaults(); | |
foreach($new_instance as $k=>$v){ | |
$instance[$k] = strip_tags($v); | |
if (empty($instance[$k]) && !empty($def[$k])){ | |
$instance[$k]=$def[$k]; | |
} | |
} | |
return $instance; | |
} | |
/*********************************************************** | |
** Utility Functions | |
***********************************************************/ | |
/** Return an input type=text field ready for admin dashboard */ | |
function w_form_input($instance, $name, $title=null){ | |
if (is_null($title)) { $title=ucwords($name); } | |
return '<label for="'.$this->get_field_id($name).'">'.__($title).'</label>'. | |
'<input class="widefat" type="text" id="'.$this->get_field_id($name).'" name="'.$this->get_field_name($name).'" value="'.esc_attr($instance[$name]).'"/>'; | |
} | |
/** Return a textarea ready for admin dashboard */ | |
function w_form_textarea($instance, $name, $title=null){ | |
if (is_null($title)) { $title=ucwords($name); } | |
return '<label for="'.$this->get_field_id($name).'">'.__($title).'</label>'. | |
'<textarea class="widefat" id="'.$this->get_field_id($name).'" name="'.$this->get_field_name($name).'">'.esc_attr($instance[$name]).'</textarea>'; | |
} | |
/** Simplify wp_parse_args */ | |
function parse_args($instance){ | |
return wp_parse_args( (array)$instance, $this->w_defaults()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment