Skip to content

Instantly share code, notes, and snippets.

@mzalewski
Created January 26, 2017 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzalewski/35cece58756f31b24ee1cc5a4f2ef9b2 to your computer and use it in GitHub Desktop.
Save mzalewski/35cece58756f31b24ee1cc5a4f2ef9b2 to your computer and use it in GitHub Desktop.
<?php
class jpen_Example_Widget extends WP_Widget {
// Set up the widget name and description.
public function __construct() {
$widget_options = array( 'classname' => 'example_widget', 'description' => 'This is an Example Widget' );
parent::__construct( 'example_widget', 'Example Widget', $widget_options );
}
// Create the widget output.
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$blog_title = get_bloginfo( 'name' );
$tagline = get_bloginfo( 'description' );
echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
<p><strong>Site Name:</strong> <?php echo $blog_title ?></p>
<p><strong>Tagline:</strong> <?php echo $tagline ?></p>
<?php echo $args['after_widget'];
}
// Create the admin area widget settings form.
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : ''; ?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
</p><?php
}
// Apply settings to the widget instance.
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
return $instance;
}
}
// Register the widget.
function jpen_register_example_widget() {
register_widget( 'jpen_Example_Widget' );
}
add_action( 'widgets_init', 'jpen_register_example_widget' );
<?php
// Place the following line at the bottom of your functions.php:
include( dirname(__FILE__) . "/example-widget.php" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment