Skip to content

Instantly share code, notes, and snippets.

@jrobinsonc
Last active May 7, 2016 16:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrobinsonc/864573da17a8dc84a7ffcd5058b7b344 to your computer and use it in GitHub Desktop.
Save jrobinsonc/864573da17a8dc84a7ffcd5058b7b344 to your computer and use it in GitHub Desktop.
WP_Widget_Ex: An easy way to create widgets in WordPress.

WP_Widget_Ex

An easy way to create widgets in WordPress

Usage

First: Require the WP_Widget_Ex class.

require_once get_template_directory() . '/inc/wp_widget_ex.php';

Second: Build your widget's class.

class WP_Example_Widget extends WP_Widget_Ex {

    /**
     * Title in WP Admin Widgets area.
     * @var string
     */
    public $widget_title = 'Example Widget';

    /**
     * Description in WP Admin Widgets area.
     * @var string
     */
    public $widget_description = 'Your site’s best widget.';

    /**
     * Widget fields details. 
     * @var array
     */
    public $widget_fields = [
        
        // Key for identify the input (allow letters, numbers and underscores).
        'title' => [
            'The best widget', // Default value for the input.
            'strip_tags|trim|esc_attr' // Callbacks functions to format the input.
        ],

        'text' => [
            'Lorem ipsum dolor sit amet.', 
            'strip_tags'
        ],
    ];

    /**
     * Widget fields inputs.
     * @var array
     */
    public $widget_fields_inputs = [

        // Key. The same of the "$this->widget_fields" var.
        'title' => [
            'Title',  // Label for the input
            '<input class="widefat" id="%1$s" name="%2$s" type="text" value="%3$s" />' // Input HTML.
        ],

        'text' => [
            'Text', 
            '<textarea class="widefat" id="%1$s" name="%2$s">%3$s</textarea>'
        ],
    ];

    /**
     * Function to show the widget in the front-end area.
     * 
     * @param  array $args
     * @param  array $instance
     * @return void
     */
    public function widget($args, $instance) 
    {
        $widget_fields = $this->get_fields($instance);

        
        echo $args['before_widget'];


        echo $args['before_title'], $widget_fields['title'], $args['after_title'];

        echo '<p>', $widget_fields['text'], '</p>';


        echo $args['after_widget'];

    }
}

Last: Register your widget.

add_action('widgets_init', function(){
    register_widget('WP_Example_Widget');
});
<?php
/**
* WP_Widget_Ex
*
* @author JoseRobinson.com
* @link https://gist.github.com/jrobinsonc/864573da17a8dc84a7ffcd5058b7b344
* @version 201605071216
*/
class WP_Widget_Ex extends WP_Widget
{
public function __construct()
{
if (! isset($this->widget_slug))
$this->widget_slug = strtolower(substr(get_class($this), 3));
$widget_ops = array(
'classname' => $this->widget_slug,
'description' => $this->widget_description
);
parent::__construct($this->widget_slug, __($this->widget_title), $widget_ops);
}
public function get_fields($instance)
{
$output = [];
foreach ($this->widget_fields as $key => $value)
$output[$key] = isset($instance[$key]) && !empty($instance[$key])? $instance[$key] : $value;
return $output;
}
public function update($new_instance, $old_instance)
{
$instance = $old_instance;
foreach ($this->widget_fields as $key => $value)
{
$instance[$key] = $new_instance[$key];
if (isset($value[1]))
{
foreach(explode('|', $value[1]) as $format)
{
$instance[$key] = $format($instance[$key]);
}
}
}
return $instance;
}
public function form($instance)
{
if (!isset($this->widget_fields_inputs))
return;
foreach ($this->widget_fields_inputs as $key => $input)
{
echo '<p>';
printf('<label for="%s">%s:</label> ',
$this->get_field_id($key),
$input[0]
);
printf($input[1],
$this->get_field_id($key),
$this->get_field_name($key),
isset($instance[$key]) ? $instance[$key] : $this->widget_fields[$key][0]
);
echo '</p>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment