Purpose:
To aid in the creation of WordPress Widgets.
Overview:
Class Name: MP_CORE_Widget. It extends the WP_Widget class and handles all of the form processing for options in the widget. It also has some built-in callback functions for field types such as color pickers, media uploads and more.
Essentially, this class reduces the amount of code-per-widget and should speed up the production of new widgets.
Example Widget
To use this sample widget, paste this class into a PHP file in your plugin.
/** * Extends MP_CORE_Widget to create custom widget class. */ class MY_Widget extends MP_CORE_Widget { /** * Register widget with WordPress. */ public function __construct() { parent::__construct( 'my_custom_widget', // Base ID 'My Widget Title', // Name array( 'description' => __( 'My Widget Description', 'textdomain' ) ) // Args ); //enqueue scripts defined in MP_CORE_Widget add_action( 'admin_enqueue_scripts', array( $this, 'mp_widget_enqueue_scripts' ) ); $this->_form = array ( "field1" => array( 'field_id' => 'title', 'field_title' => __('Title:', 'textdomain'), 'field_description' => NULL, 'field_type' => 'textbox', ), "field2" => array( 'field_id' => 'custom_field', 'field_title' => __('Custom Field:', 'textdomain'), 'field_description' => NULL, 'field_type' => 'select', 'field_select_values' => array( 'option1' => "Option 1" ), ) ); } /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { //Set the Variables $title = $instance['title']; $custom_field = $instance['custom_field'] /** * Widget Start and Title */ echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; /** * Widget Body */ echo $title; echo $custom_field; /** * Widget End */ echo $after_widget; } } add_action( 'register_sidebar', create_function( '', 'register_widget( "MY_Widget" );' ) );
Usage Breakdown:
To use this class, you need to create your own class which extends the MP_CORE_Widget class which extends the WP_Widget class. It’s starting to sound a little bit complicated and inception-like, but it’s actually fairly simple.
Start by creating the new class and extending the MP_CORE_Widget class. Also, open the construct function for this class.
class MY_Widget extends MP_CORE_Widget { public function __construct() {
Next, set the construct function up which passes some info to the parent class. This includes the widget id, the widget Title, and the widget description as seen here:
parent::__construct( 'my_custom_widget', 'My Widget Title', array( 'description' => __( 'My Widget Description', 'text-domain' ) ) // Args );
Next, while still inside the construct function, enqueue the scripts used for things like colorpickers and image upload.
//enqueue scripts defined in MP_CORE_Widget add_action( 'admin_enqueue_scripts', array( $this, 'mp_widget_enqueue_scripts' ) );
And finally, while still in the construct function, create the form used for the widget’s options. It is an associative array which is set up for each field to have an id, a title, a description, and a type. Field types are things like textbox, colorpicker, mediaupload, or selects.
$this->_form = array ( "field1" => array( 'field_id' => 'title', 'field_title' => __('Title:', 'mp_slide'), 'field_description' => NULL, 'field_type' => 'textbox', ) );
And then we end the constructor function
}
Widget Field Types:
- textbox
- url
- date
- password
- number
- select
- checkbox
- textarea
- colorpicker
- mediaupload
Usage of Widget Field Types
Textbox:
"field1" => array( 'field_id' => 'my_field_id', 'field_title' => __('My Field Title:', 'textdomain'), 'field_description' => __('My Field Description:', 'textdomain'), 'field_type' => 'textbox', ),
URL:
"field1" => array( 'field_id' => 'my_field_id', 'field_title' => __('My Field Title:', 'textdomain'), 'field_description' => __('My Field Description:', 'textdomain'), 'field_type' => 'url', ),
Date:
"field1" => array( 'field_id' => 'my_field_id', 'field_title' => __('My Field Title:', 'textdomain'), 'field_description' => __('My Field Description:', 'textdomain'), 'field_type' => 'date', ),
Password:
"field1" => array( 'field_id' => 'my_field_id', 'field_title' => __('My Field Title:', 'textdomain'), 'field_description' => __('My Field Description:', 'textdomain'), 'field_type' => 'password', ),
Number:
"field1" => array( 'field_id' => 'my_field_id', 'field_title' => __('My Field Title:', 'textdomain'), 'field_description' => __('My Field Description:', 'textdomain'), 'field_type' => 'number', ),
Select:
"field1" => array( 'field_id' => 'my_field_id', 'field_title' => __('My Field Title:', 'textdomain'), 'field_description' => __('My Field Description:', 'textdomain'), 'field_type' => 'select', 'field_select_values' => array( 'option_value_1' => 'Option Name 1', 'option_value_2' => 'Option Name 2' ), ),
Checkbox:
"field1" => array( 'field_id' => 'my_field_id', 'field_title' => __('My Field Title:', 'textdomain'), 'field_description' => __('My Field Description:', 'textdomain'), 'field_type' => 'checkbox', ),
Text Area:
"field1" => array( 'field_id' => 'my_field_id', 'field_title' => __('My Field Title:', 'textdomain'), 'field_description' => __('My Field Description:', 'textdomain'), 'field_type' => 'textarea', ),
Color Picker:
"field1" => array( 'field_id' => 'my_field_id', 'field_title' => __('My Field Title:', 'textdomain'), 'field_description' => __('My Field Description:', 'textdomain'), 'field_type' => 'colorpicker', ),
Media Upload:
"field1" => array( 'field_id' => 'my_field_id', 'field_title' => __('My Field Title:', 'textdomain'), 'field_description' => __('My Field Description:', 'textdomain'), 'field_type' => 'mediaupload', ),