Skip to content

Instantly share code, notes, and snippets.

@BAProductions
Forked from isGabe/widgets.php
Created November 9, 2024 00:41
Show Gist options
  • Save BAProductions/11c162f7bf564c35600f4aa5ba2368b8 to your computer and use it in GitHub Desktop.
Save BAProductions/11c162f7bf564c35600f4aa5ba2368b8 to your computer and use it in GitHub Desktop.
Basic WordPress Widget Code #snippet #WordPress
<?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