Created
March 5, 2014 20:15
-
-
Save grappler/9375663 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Plugin Name. | |
* | |
* @package Plugin_Name | |
* @author Your Name <[email protected]> | |
* @license GPL-2.0+ | |
* @link http://example.com | |
* @copyright 2014 Your Name or Company Name | |
*/ | |
// TODO: change 'Widget_Name' to the name of your plugin | |
class Widget_Name extends WP_Widget { | |
/*--------------------------------------------------*/ | |
/* Constructor | |
/*--------------------------------------------------*/ | |
/** | |
* Specifies the classname and description, instantiates the widget, | |
* loads localization files, and includes necessary stylesheets and JavaScript. | |
*/ | |
public function __construct() { | |
// TODO: update widget-name-id, classname and description | |
// TODO: replace 'widget-name-locale' to be named more plugin specific. Other instances exist throughout the code, too. | |
parent::__construct( | |
'widget-name-id', // <-- Change this | |
__( 'Widget Name', 'plugin-name' ), | |
array( | |
'classname' => 'widget-name-class', | |
'description' => __( 'Short description of the widget goes here.', 'plugin-name' ) | |
) | |
); | |
// Refreshing the widget's cached output with each new post | |
add_action( 'save_post', array( $this, 'flush_widget_cache' ) ); | |
add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) ); | |
add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) ); | |
} // end constructor | |
/*--------------------------------------------------*/ | |
/* Widget API Functions | |
/*--------------------------------------------------*/ | |
/** | |
* Outputs the content of the widget. | |
* | |
* @param array args The array of form elements | |
* @param array instance The current instance of the widget | |
*/ | |
public function widget( $args, $instance ) { | |
// Check if there is a cached output | |
$cache = wp_cache_get( 'widget-name-id', 'widget' ); | |
if ( !is_array( $cache ) ) | |
$cache = array(); | |
if ( ! isset ( $args['widget_id'] ) ) | |
$args['widget_id'] = $this->id; | |
if ( isset ( $cache[ $args['widget_id'] ] ) ) | |
return print $cache[ $args['widget_id'] ]; | |
// go on with your widget logic, put everything into a string and … | |
extract( $args, EXTR_SKIP ); | |
$widget_string = $before_widget; | |
// TODO: Here is where you manipulate your widget's values based on their input fields | |
ob_start(); | |
include( plugin_dir_path( __FILE__ ) . 'public/views/widget.php' ); | |
$widget_string .= ob_get_clean(); | |
$widget_string .= $after_widget; | |
$cache[ $args['widget_id'] ] = $widget_string; | |
wp_cache_set( 'widget-name-id', $cache, 'widget' ); | |
print $widget_string; | |
} // end widget | |
public function flush_widget_cache() { | |
wp_cache_delete( 'widget-name-id', 'widget' ); | |
} | |
/** | |
* Processes the widget's options to be saved. | |
* | |
* @param array new_instance The new instance of values to be generated via the update. | |
* @param array old_instance The previous instance of values before the update. | |
*/ | |
public function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
// TODO: Here is where you update your widget's old values with the new, incoming values | |
return $instance; | |
} // end widget | |
/** | |
* Generates the administration form for the widget. | |
* | |
* @param array instance The array of keys and values for the widget. | |
*/ | |
public function form( $instance ) { | |
// TODO: Define default values for your variables | |
$instance = wp_parse_args( | |
(array) $instance | |
); | |
// TODO: Store the values of the widget in their own variable | |
// Display the admin form | |
include( plugin_dir_path(__FILE__) . 'admin/views/widget.php' ); | |
} // end form | |
/*--------------------------------------------------*/ | |
/* Public Functions | |
/*--------------------------------------------------*/ | |
/** | |
* Registers and enqueues widget-specific styles. | |
*/ | |
public function register_widget_styles() { | |
// TODO: Change 'widget-name' to the name of your plugin | |
wp_enqueue_style( 'widget-name-widget-styles', plugins_url( 'widget-name/css/widget.css' ) ); | |
} // end register_widget_styles | |
/** | |
* Registers and enqueues widget-specific scripts. | |
*/ | |
public function register_widget_scripts() { | |
// TODO: Change 'widget-name' to the name of your plugin | |
wp_enqueue_script( 'widget-name-script', plugins_url( 'widget-name/js/widget.js' ), array('jquery') ); | |
} // end register_widget_scripts | |
} // end class | |
// TODO: Remember to change 'Widget_Name' to match the class name definition | |
add_action( 'widgets_init', create_function( '', 'register_widget("Widget_Name");' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment