Created
May 26, 2013 18:26
-
-
Save alexpos/5653610 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: MyWidget | |
Description: Description | |
Author: Pixolin | |
*/ | |
/******************** | |
* add a new Widget to WP_Widget class | |
* use a unique name by prefixing the class name | |
*/ | |
class awesome_Widget extends WP_Widget { | |
/******************** | |
* BACK END: The Widget as it appears in the back end in the left Widgets pane | |
*/ | |
function awesome_Widget() { | |
$widget_ops = array( | |
'classname' => 'awesomewidget', // CSS class for the widget Front end | |
'description' => 'Description: Widget showing Book Recommendation' // Describes, what the widget is good for | |
); | |
/* | |
* give this widget a CSS class .pix_widget for use in wp-admin, | |
* give it a title 'Awesome widget' and use $widget_ops as set above | |
*/ | |
$this->WP_Widget( 'pix_widget', 'Awesome widget', $widget_ops ); | |
} | |
/******************** | |
* BACK END FORM when dragged into the preserved place, e.g. sidebar | |
* $instance is a unique number for various instances of the same widget being used | |
*/ | |
function form( $instance ) { | |
//Defaults in case nothing is set | |
$defaults = array( | |
'title'=> 'Book of the Day', | |
'book' => 'Robinson Crusoe'); | |
$instance = wp_parse_args( (array) $instance, $defaults ); | |
$title = $instance['title']; | |
$book = $instance['book']; | |
?> | |
<p>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"></p> | |
<p>Book: <input class="widefat" name="<?php echo $this->get_field_name( 'book' ); ?>" type="text" value="<?php echo esc_attr( $book ); ?>"></p> | |
<?php | |
} | |
/******************** | |
* SAVE the updates made in the BACK END FORM | |
*/ | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['title']= strip_tags( $new_instance['title'] ); | |
$instance['book']= strip_tags( $new_instance['book'] ); | |
return $instance; | |
} | |
/******************** | |
* OUTPUT of the Widget Frontend, using $args (settings *what* to display) and | |
* $instance (settings for which instance of the plugin the $args were set) | |
* $args holds value such as the tag being used 'before_widget', but also arguments | |
* for e.g. a query | |
*/ | |
function widget( $args, $instance ) { | |
// first we need to extract the associative array with information what tags to use before and after widget into single variables... | |
extract( $args ); | |
// we use here extracted variables here | |
echo $before_widget; | |
// we want to display the value of variable $book for this $instance of the widget | |
$title = $instance['title']; | |
$book = $instance['book']; | |
if (!empty($title)) { | |
echo $before_title; //tag before title | |
echo $title; | |
echo $after_title; //closing tag after title | |
} | |
if (!empty($book)) { | |
echo "We recommend today: <br><strong>" . $book . "</strong>"; | |
} | |
echo $after_widget; | |
} | |
} | |
/******************** | |
* Now we use a function to register the widget | |
*/ | |
function register_awesome_Widget() { | |
register_widget( 'awesome_Widget' ); | |
} | |
/******************** | |
* Finally let's hook it | |
*/ | |
add_action( 'widgets_init', 'register_awesome_Widget' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment