Created
June 17, 2013 20:25
-
-
Save isGabe/5800027 to your computer and use it in GitHub Desktop.
Basic WordPress Widget Code #snippet #WordPress
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 | |
/* | |
* Basic WordPress Widget Code | |
* Save to: /wp-content/theme/widgets.php | |
* in functions.php or appropriate place: if ($wp_version >= 2.8) require_once(TEMPLATEPATH.'/widgets.php'); | |
* | |
*/ | |
class MyWidgetName extends WP_Widget | |
{ | |
function MyWidgetName() | |
{ | |
$widget_ops = array('classname' => 'MyWidgetName', 'description' => 'My Widget Description' ); | |
$this->WP_Widget('MyWidgetName', 'My Widget Title', $widget_ops); | |
} | |
function form($instance) | |
{ | |
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) ); | |
$title = $instance['title']; | |
?> | |
<p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p> | |
<?php | |
} | |
function update($new_instance, $old_instance) | |
{ | |
$instance = $old_instance; | |
$instance['title'] = $new_instance['title']; | |
return $instance; | |
} | |
function widget($args, $instance) | |
{ | |
extract($args, EXTR_SKIP); | |
echo $before_widget; | |
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']); | |
if (!empty($title)) | |
echo '<h3 class="widget-title">' . $title . '</h3>'; | |
// WIDGET CODE GOES HERE | |
} | |
} | |
add_action( 'widgets_init', create_function('', 'return register_widget("MyWidgetName");') ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment