Skip to content

Instantly share code, notes, and snippets.

@bueltge
Created October 3, 2011 18:46
Show Gist options
  • Save bueltge/1259892 to your computer and use it in GitHub Desktop.
Save bueltge/1259892 to your computer and use it in GitHub Desktop.
Widget Basis
<?php
/**
* Widget
* @author
* @package
*/
// TODO: change 'Fb_Wp_Widget_Basis' to the name of your actual plugin
class Fb_Wp_Widget_Basis extends WP_Widget {
/**
* TODO: update these values to reflect the name, locale, and slug
* of your plugin.
*/
const name = 'Fb_Wp_Widget_Basis';
const locale = 'Fb_Wp_Widget_Basis_locale';
const slug = 'Fb_Wp_Widget_Basis_slug';
/**
* Constructor
*
* The widget constructor. Specifies the classname and description, instantiates
* the widget, loads localization files, and includes necessary scripts and
* styles.
*/
// TODO: This should match the title given in the class definition above.
function Fb_Wp_Widget_Basis () {
load_plugin_textdomain(
self :: locale,
FALSE,
dirname( plugin_basename( __FILE__ ) ) . '/languages/'
);
// TODO: update classname and description
$widget_opts = array (
'classname' => self :: name,
'description' => __( 'Short description of the plugin goes here.', self :: locale )
);
$this -> WP_Widget( self :: slug, __( self :: name, self :: locale ), $widget_opts );
// Load JavaScript and stylesheets
$this -> register_scripts_and_styles();
}
/**
* API Functions, Outputs the content of the widget.
*
* @args The array of form elements
* @instance
*/
function widget ( $args, $instance ) {
extract( $args, EXTR_SKIP );
echo $before_widget;
// TODO: This is where you retrieve the widget values
// Display the widget
include( WP_PLUGIN_DIR . '/' . self :: slug . '/inc/widget.php' );
echo $after_widget;
}
/**
* Processes the widget's options to be saved.
*
* @new_instance The previous instance of values before the update.
* @old_instance The new instance of values to be generated via the update.
*/
function update ( $new_instance, $old_instance ) {
$instance = $old_instance;
// TODO Update the widget with the new values
return $instance;
}
/**
* Generates the administration form for the widget.
*
* @instance The array of keys and values for the widget.
*/
function form ( $instance ) {
// TODO define default values for your variables
$instance = wp_parse_args(
(array) $instance,
array(
'' => ''
)
);
// TODO store the values of widget in a variable
// Display the admin form
include( WP_PLUGIN_DIR . '/' . self :: slug . '/inc/admin.php' );
} // end form
/**
* Private Functions, Registers and enqueues stylesheets for the administration panel and the
* public facing site.
*/
private function register_scripts_and_styles () {
if ( is_admin() ) {
// includes files for admin of WP
$this -> load_file( PLUGIN_NAME, '/' . self :: slug . '/js/admin.js', TRUE );
$this -> load_file( PLUGIN_NAME, '/' . self :: slug . '/css/admin.css' );
} else {
// include files for frontend if WP
$this -> load_file( PLUGIN_NAME, '/' . self :: slug . '/js/widget.js', TRUE );
$this -> load_file( PLUGIN_NAME, '/' . self :: slug . '/css/widget.css' );
}
}
/**
* Helper function for registering and enqueueing scripts and styles.
*
* @name The ID to register with WordPress
* @file_path The path to the actual file
* @is_script Optional argument for if the incoming file_path is a JavaScript source file.
*/
private function load_file ( $name, $file_path, $is_script = FALSE ) {
$url = WP_PLUGIN_URL . $file_path;
$file = WP_PLUGIN_DIR . $file_path;
if ( file_exists( $file ) ) {
if ( $is_script ) {
wp_register_script( $name, $url );
wp_enqueue_script( $name );
} else {
wp_register_style( $name, $url );
wp_enqueue_style( $name );
}
}
}
} // end class
add_action( 'widgets_init', create_function( '', 'register_widget( "Fb_Wp_Widget_Basis" );' ) );
// TODO remember to change this to match the class definition above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment