Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Plugin code to create a single widget in wordpress.
<?php
/**
* @package RedRokk
* @version 1.0.0
*
* Plugin Name: Empty Widget
* Description: Single Widget Class handles all of the widget responsibility, all that you need to do is create the html. Just use Find/Replace on the Empty_Widget keyword to rebrand this class for your needs.
* Author: RedRokk Interactive Media
* Version: 1.0.0
* Author URI: http://www.redrokk.com
*/
/**
* Protection
*
* This string of code will prevent hacks from accessing the file directly.
*/
defined('ABSPATH') or die("Cannot access pages directly.");
/**
* Initializing
*
* The directory separator is different between linux and microsoft servers.
* Thankfully php sets the DIRECTORY_SEPARATOR constant so that we know what
* to use.
*/
defined("DS") or define("DS", DIRECTORY_SEPARATOR);
/**
* Actions and Filters
*
* Register any and all actions here. Nothing should actually be called
* directly, the entire system will be based on these actions and hooks.
*/
add_action( 'widgets_init', create_function( '', 'register_widget("Empty_Widget");' ) );
/**
*
* @author Senior Software Programmer @ RedRokk : Jonathon Byrd
*
*/
class Empty_Widget extends WP_Widget
{
/**
* Widget settings
*
* Simply use the following field examples to create the WordPress Widget options that
* will display to administrators. These options can then be found in the $params
* variable within the widget method.
*
*
array(
'name' => 'Title',
'desc' => '',
'id' => 'title',
'type' => 'text',
'std' => 'Your widgets title'
),
array(
'name' => 'Textarea',
'desc' => 'Enter big text here',
'id' => 'textarea_id',
'type' => 'textarea',
'std' => 'Default value 2'
),
array(
'name' => 'Select box',
'desc' => '',
'id' => 'select_id',
'type' => 'select',
'options' => array( 'KEY1' => 'Value 1', 'KEY2' => 'Value 2', 'KEY3' => 'Value 3' )
),
array(
'name' => 'Radio',
'desc' => '',
'id' => 'radio_id',
'type' => 'radio',
'options' => array(
array('name' => 'Name 1', 'value' => 'Value 1'),
array('name' => 'Name 2', 'value' => 'Value 2')
)
),
array(
'name' => 'Checkbox',
'desc' => '',
'id' => 'checkbox_id',
'type' => 'checkbox'
),
*/
protected $widget = array(
// this description will display within the administrative widgets area
// when a user is deciding which widget to use.
'description' => 'Single Widget Class handles all of the widget responsibility, all that you need to do is create the html and change the description. RedRokk',
// determines whether or not to use the sidebar _before and _after html
'do_wrapper' => true,
// string : if you set a filename here, it will be loaded as the view
// when using a file the following array will be given to the file :
// array('widget'=>array(),'params'=>array(),'sidebar'=>array(),
// alternatively, you can return an html string here that will be used
'view' => false,
'fields' => array(
// You should always offer a widget title
array(
'name' => 'Title',
'desc' => '',
'id' => 'title',
'type' => 'text',
'std' => 'Widget Title'
),
)
);
/**
* Widget HTML
*
* If you want to have an all inclusive single widget file, you can do so by
* dumping your css styles with base_encoded images along with all of your
* html string, right into this method.
*
* @param array $widget
* @param array $params
* @param array $sidebar
*/
function html($widget, $params, $sidebar)
{
?>
<!-- Your widget html goes here -->
<?php
}
/**
* Constructor
*
* Registers the widget details with the parent class, based off of the options
* that were defined within the widget property. This method does not need to be
* changed.
*/
function Empty_Widget()
{
//Initializing
$classname = str_replace('_',' ', get_class($this));
// widget actual processes
parent::WP_Widget(
$id = (isset($this->widget['description'])?$this->widget['description']:$classname),
$name = (isset($this->widget['name'])?$this->widget['name']:$classname),
$options = array( 'description'=>$this->widget['description'] )
);
}
/**
* Widget View
*
* This method determines what view method is being used and gives that view
* method the proper parameters to operate. This method does not need to be
* changed.
*
* @param array $sidebar
* @param array $params
*/
function widget($sidebar, $params)
{
//initializing variables
$this->widget['number'] = $this->number;
$title = apply_filters( 'Empty_Widget_title', $params['title'] );
$do_wrapper = (!isset($this->widget['do_wrapper']) || $this->widget['do_wrapper']);
if ( $do_wrapper )
echo $sidebar['before_widget'];
//loading a file that is isolated from other variables
if (file_exists($this->widget['view']))
$this->getViewFile($widget, $params, $sidebar);
if ($this->widget['view'])
echo $this->widget['view'];
else $this->html($this->widget, $params, $sidebar);
if ( $do_wrapper )
echo $sidebar['after_widget'];
}
/**
* Get the View file
*
* Isolates the view file from the other variables and loads the view file,
* giving it the three parameters that are needed. This method does not
* need to be changed.
*
* @param array $widget
* @param array $params
* @param array $sidebar
*/
function getViewFile($widget, $params, $sidebar) {
require $this->widget['view'];
}
/**
* Administration Form
*
* This method is called from within the wp-admin/widgets area when this
* widget is placed into a sidebar. The resulting is a widget options form
* that allows the administration to modify how the widget operates.
*
* You do not need to adjust this method what-so-ever, it will parse the array
* parameters given to it from the protected widget property of this class.
*
* @param array $instance
* @return boolean
*/
function form($instance)
{
//reasons to fail
if (empty($this->widget['fields'])) return false;
$defaults = array(
'id' => '',
'name' => '',
'desc' => '',
'type' => '',
'options' => '',
'std' => '',
);
do_action('Empty_Widget_before');
foreach ($this->widget['fields'] as $field)
{
//making sure we don't throw strict errors
$field = wp_parse_args($field, $defaults);
$meta = false;
if (isset($field['id']) && array_key_exists($field['id'], $instance))
@$meta = attribute_escape($instance[$field['id']]);
if ($field['type'] != 'custom' && $field['type'] != 'metabox')
{
echo '<p><label for="',$this->get_field_id($field['id']),'">';
}
if (isset($field['name']) && $field['name']) echo $field['name'],':';
switch ($field['type'])
{
case 'text':
echo '<input type="text" name="', $this->get_field_name($field['id']), '" id="', $this->get_field_id($field['id']), '" value="', ($meta ? $meta : @$field['std']), '" class="vibe_text" />',
'<br/><span class="description">', @$field['desc'], '</span>';
break;
case 'textarea':
echo '<textarea class="vibe_textarea" name="', $this->get_field_name($field['id']), '" id="', $this->get_field_id($field['id']), '" cols="60" rows="4" style="width:97%">', $meta ? $meta : @$field['std'], '</textarea>',
'<br/><span class="description">', @$field['desc'], '</span>';
break;
case 'select':
echo '<select class="vibe_select" name="', $this->get_field_name($field['id']), '" id="', $this->get_field_id($field['id']), '">';
foreach ($field['options'] as $value => $option)
{
$selected_option = ( $value ) ? $value : $option;
echo '<option', ($value ? ' value="' . $value . '"' : ''), ($meta == $selected_option ? ' selected="selected"' : ''), '>', $option, '</option>';
}
echo '</select>',
'<br/><span class="description">', @$field['desc'], '</span>';
break;
case 'radio':
foreach ($field['options'] as $option)
{
echo '<input class="vibe_radio" type="radio" name="', $this->get_field_name($field['id']), '" value="', $option['value'], '"', ($meta == $option['value'] ? ' checked="checked"' : ''), ' />',
$option['name'];
}
echo '<br/><span class="description">', @$field['desc'], '</span>';
break;
case 'checkbox':
echo '<input type="hidden" name="', $this->get_field_name($field['id']), '" id="', $this->get_field_id($field['id']), '" /> ',
'<input class="vibe_checkbox" type="checkbox" name="', $this->get_field_name($field['id']), '" id="', $this->get_field_id($field['id']), '"', $meta ? ' checked="checked"' : '', ' /> ',
'<br/><span class="description">', @$field['desc'], '</span>';
break;
case 'custom':
echo $field['std'];
break;
}
if ($field['type'] != 'custom' && $field['type'] != 'metabox')
{
echo '</label></p>';
}
}
do_action('Empty_Widget_after');
return true;
}
/**
* Update the Administrative parameters
*
* This function will merge any posted paramters with that of the saved
* parameters. This ensures that the widget options never get lost. This
* method does not need to be changed.
*
* @param array $new_instance
* @param array $old_instance
* @return array
*/
function update($new_instance, $old_instance)
{
// processes widget options to be saved
$instance = wp_parse_args($new_instance, $old_instance);
return $instance;
}
}
<?php
/**
* @Author Jonathon byrd
* @link http://www.jonathonbyrd.com
* @Package Wordpress
* @SubPackage Widgets
* @copyright Proprietary Software, Copyright Byrd Incorporated. All Rights Reserved
* @Since 1.0.0
*
* Plugin Name: Master Widget
* Plugin URI: http://www.redrokk.com
* Description: <a href="http://redrokk.com" target="_blank">redrokk</a> Designs and develops software for WordPress, Drupal, Joomla, Cakephp, SugarCRM, Symfony and more!
* Version: 1.0.0
* Author: redrokk
* Author URI: http://www.redrokk.com
*
*
*/
defined('ABSPATH') or die("Cannot access pages directly.");
/**
* Register a New Master Widget
*
* The following is an array of settings for a single widget.
* All that you need to worry about is defining this array and the
* logic + administrative options for the widget is handled.
*
* The actual display of the widget is not handled by the Master
* Widget Class and requires that you provide a callback or a file that
* can be displayed when the widget is shown on the front end.
*
* A nice array of values is provided to you when displaying the widget
* UI, simply use extract($args) to retrieve three variables full of
* useful data.
*
* The following code should be placed within your theme/functions.php
*
* ************** ************* *************
//build an array of settings
$docWidget = array(
'id' => 'first-custom-widget', //make sure that this is unique
'title' => 'aaaFirst Widget',
'classname' => 'st-custom-wi',
'do_wrapper' => true,
'show_view' => 'document_widget_view',
'fields' => array(
array(
'name' => 'Title',
'desc' => '',
'id' => 'title',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Textarea',
'desc' => 'Enter big text here',
'id' => 'textarea_id',
'type' => 'textarea',
'std' => 'Default value 2'
),
array(
'name' => 'Select box',
'id' => 'select_id',
'type' => 'select',
'options' => array( 'KEY1' => 'Value 1', 'KEY2' => 'Value 2', 'KEY3' => 'Value 3' )
),
array(
'name' => 'Radio',
'id' => 'radio_id',
'type' => 'radio',
'options' => array(
array('name' => 'Name 1', 'value' => 'Value 1'),
array('name' => 'Name 2', 'value' => 'Value 2')
)
),
array(
'name' => 'Checkbox',
'id' => 'checkbox_id',
'type' => 'checkbox'
),
)
);
//register this widget
register_master_widget($docWidget);
function document_widget_view( $args )
{
extract($args);
?>
the view for my widget
<?php
}
*/
/**
* Only display once
*
* This line of code will ensure that we only run the master widget class
* a single time. We don't need to be throwing errors.
*/
if (!class_exists('MasterWidgetClass')) :
/**
* Initializing
*
* The directory separator is different between linux and microsoft servers.
* Thankfully php sets the DIRECTORY_SEPARATOR constant so that we know what
* to use.
*/
defined("DS") or define("DS", DIRECTORY_SEPARATOR);
/**
* Actions and Filters
*
* Register any and all actions here. Nothing should actually be called
* directly, the entire system will be based on these actions and hooks.
*/
add_action( 'widgets_init', 'widgets_init_declare_registered', 1 );
/**
* Register a widget
*
* @param $widget
*/
function register_master_widget( $widget = null )
{
global $masterWidgets;
if (!isset($masterWidgets))
{
$masterWidgets = array();
}
if (!is_array($widget)) return false;
$defaults = array(
'id' => '1',
'title' => 'Generic Widget',
'classname' => $widget['id'],
'do_wrapper' => true,
'description' => '',
'width' => 200,
'height' => 200,
'fields' => array(),
);
$masterWidgets[$widget['id']] = wp_parse_args($widget, $defaults);
return true;
}
/**
* Get the registered widgets
*
* @return array
*/
function get_registered_masterwidgets()
{
global $masterWidgets;
if (!did_action('init_master_widgets'))
do_action('init_master_widgets');
return $masterWidgets;
}
/**
* Initialize the widgets
*
* @return boolean
*/
function widgets_init_declare_registered()
{
//initialziing variables
global $wp_widget_factory;
$widgets = get_registered_masterwidgets();
//reasons to fail
if (empty($widgets) || !is_array($widgets)) return false;
foreach ($widgets as $id => $widget)
{
$wp_widget_factory->widgets[$id] =& new MasterWidgetClass( $widget );
}
return false;
}
/**
* Multiple Widget Master Class
*
* This class allows us to easily create qidgets without having to deal with the
* mass of php code.
*
* @author byrd
* @since 1.3
*/
class MasterWidgetClass extends WP_Widget
{
/**
* Constructor.
*
* @param $widget
*/
function MasterWidgetClass( $widget )
{
$this->widget = apply_filters('master_widget_setup', $widget);
// Added Widget Admin Settings.
$widget_ops = array(
'classname' => $this->widget['classname'],
'description' => $this->widget['description']
);
// Added Widget Control Settings.
$control_ops = array(
'width' => $this->widget['width'],
'height' => $this->widget['height']
);
$this->WP_Widget($this->widget['id'], $this->widget['title'], $widget_ops, $control_ops);
}
/**
* Display the Widget View
*
* @example extract the args within the view template
extract($args[1]);
* @param $args
* @param $instance
*/
function widget($sidebar, $instance)
{
//initializing variables
$widget = $this->widget;
$widget['number'] = $this->number;
$args = array(
'sidebar' => $sidebar,
'widget' => $widget,
'params' => $instance,
);
$show_view = apply_filters('master_widget_view', $this->widget['show_view'], $widget, $instance, $sidebar);
$title = apply_filters( 'master_widget_title', $instance['title'] );
if ( $widget['do_wrapper'] )
echo $sidebar['before_widget'];
if ( $title && $widget['do_wrapper'] )
echo $sidebar['before_title'] . $title . $sidebar['after_title'];
//give people an opportunity
do_action('master_widget_show_'.$widget['id']);
//load the file if it exists
if (file_exists($show_view))
require $show_view;
//call the function if it exists
elseif (is_callable($show_view))
call_user_func( $show_view, $args );
//echo if we can't figure it out
else echo $show_view;
if ($widget['do_wrapper'])
echo $sidebar['after_widget'];
}
/**
* Update from within the admin
*
* @param $new_instance
* @param $old_instance
*/
function update($new_instance, $old_instance)
{
//initializing variables
$new_instance = array_map('strip_tags', $new_instance);
$instance = wp_parse_args($new_instance, $old_instance);
return $instance;
}
/**
* Display the options form
*
* @param $instance
*/
function form($instance)
{
//reasons to fail
if (empty($this->widget['fields'])) return false;
$defaults = array(
'id' => '',
'name' => '',
'desc' => '',
'type' => '',
'options' => '',
'std' => '',
);
do_action('master_widget_before');
foreach ($this->widget['fields'] as $field)
{
//making sure we don't throw strict errors
$field = wp_parse_args($field, $defaults);
$meta = false;
if (isset($field['id']) && array_key_exists($field['id'], $instance))
@$meta = attribute_escape($instance[$field['id']]);
if ($field['type'] != 'custom' && $field['type'] != 'metabox')
{
echo '<p><label for="',$this->get_field_id($field['id']),'">';
}
if (isset($field['name']) && $field['name']) echo $field['name'],':';
switch ($field['type'])
{
case 'text':
echo '<input type="text" name="', $this->get_field_name($field['id']), '" id="', $this->get_field_id($field['id']), '" value="', ($meta ? $meta : @$field['std']), '" class="vibe_text" />',
'<br/><span class="description">', @$field['desc'], '</span>';
break;
case 'textarea':
echo '<textarea class="vibe_textarea" name="', $this->get_field_name($field['id']), '" id="', $this->get_field_id($field['id']), '" cols="60" rows="4" style="width:97%">', $meta ? $meta : @$field['std'], '</textarea>',
'<br/><span class="description">', @$field['desc'], '</span>';
break;
case 'select':
echo '<select class="vibe_select" name="', $this->get_field_name($field['id']), '" id="', $this->get_field_id($field['id']), '">';
foreach ($field['options'] as $value => $option)
{
$selected_option = ( $value ) ? $value : $option;
echo '<option', ($value ? ' value="' . $value . '"' : ''), ($meta == $selected_option ? ' selected="selected"' : ''), '>', $option, '</option>';
}
echo '</select>',
'<br/><span class="description">', @$field['desc'], '</span>';
break;
case 'radio':
foreach ($field['options'] as $option)
{
echo '<input class="vibe_radio" type="radio" name="', $this->get_field_name($field['id']), '" value="', $option['value'], '"', ($meta == $option['value'] ? ' checked="checked"' : ''), ' />',
$option['name'];
}
echo '<br/><span class="description">', @$field['desc'], '</span>';
break;
case 'checkbox':
echo '<input type="hidden" name="', $this->get_field_name($field['id']), '" id="', $this->get_field_id($field['id']), '" /> ',
'<input class="vibe_checkbox" type="checkbox" name="', $this->get_field_name($field['id']), '" id="', $this->get_field_id($field['id']), '"', $meta ? ' checked="checked"' : '', ' /> ',
'<br/><span class="description">', @$field['desc'], '</span>';
break;
case 'custom':
echo $field['std'];
break;
}
if ($field['type'] != 'custom' && $field['type'] != 'metabox')
{
echo '</label></p>';
}
}
do_action('master_widget_after');
return;
}
}// ends Master Widget Class
endif; //if !class_exists
<?php
/**
* @Author Jonathon byrd
* @link http://www.jonathonbyrd.com
* @Package Wordpress
* @SubPackage Widgets
* @copyright Proprietary Software, Copyright Byrd Incorporated. All Rights Reserved
* @Since 1.0.0
*
* Plugin Name: Document Widget
* Plugin URI: http://www.redrokk.com
* Description: <a href="http://redrokk.com" target="_blank">redrokk</a> Designs and develops software for WordPress, Drupal, Joomla, Cakephp, SugarCRM, Symfony and more!
* Version: 1.0.0
* Author: redrokk
* Author URI: http://www.redrokk.com
*
*
*/
defined('ABSPATH') or die("Cannot access pages directly.");
/**
* Initializing
*
* The directory separator is different between linux and microsoft servers.
* Thankfully php sets the DIRECTORY_SEPARATOR constant so that we know what
* to use.
*/
defined("DS") or define("DS", DIRECTORY_SEPARATOR);
/**
* Actions and Filters
*
* Register any and all actions here. Nothing should actually be called
* directly, the entire system will be based on these actions and hooks.
*/
add_action( 'widgets_init', create_function( '', 'register_widget("Document_Widget");' ) );
/**
*
* @author byrd
* Document Widget
*/
class Document_Widget extends WP_Widget
{
/**
* Constructor
*
* Registers the widget details with the parent class
*/
function Document_Widget()
{
// widget actual processes
parent::WP_Widget( $id = 'foo_widget', $name = get_class($this), $options = array( 'description' => 'A Foo Widget' ) );
}
function form($instance)
{
// outputs the options form on admin
?>
Form goes here
<?php
}
function update($new_instance, $old_instance)
{
// processes widget options to be saved
$instance = wp_parse_args($old_instance, $new_instance);
return $instance;
}
function widget($args, $instance)
{
// outputs the content of the widget
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
?>
Widget view to users goes here
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.