Created
February 2, 2016 15:03
-
-
Save iCaspar/35629bc726443c60d30f to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* widget-demo.php | |
* | |
* Our widget demo plugin | |
* | |
* @author: Caspar Green <https://caspar.green> | |
* @package: widget-demo | |
* @version: 1.0.0 | |
*/ | |
/** | |
* @plugin | |
* Plugin Name: Widget Demo | |
* Plugin URI: http://caspar.green/ | |
* Description: A simple widget demo. | |
* Version: 1.0.0 | |
* Author: Caspar Green | |
* Author URI: http://caspar.green/ | |
*/ | |
namespace Caspar; | |
// Oh no you di'n't | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit( 'Oh no you di\'n\'t' ); | |
} | |
class Widget_Demo extends \WP_Widget { | |
protected $defaults; | |
/** | |
* Widget_Demo constructor. | |
*/ | |
public function __construct() { | |
$this->defaults = array( | |
'title' => '', | |
'image_id' => '', | |
'caption' => '', | |
'link' => '' | |
); | |
$widget_options = array( | |
'classname' => 'demo-widget', | |
'description' => __( 'A Simple Demo Widget', 'caspar' ) | |
); | |
parent::__construct( 'widget-demo', __( 'A Simple Demo Widget', 'caspar' ), $widget_options ); | |
} | |
/** | |
* Form for entering widget info in the admin. | |
* | |
* @param array $instance | |
* | |
* @return null | |
*/ | |
public function form( $instance ) { | |
$instance = wp_parse_args( $instance, $this->defaults ); | |
extract( $instance ); | |
?> | |
<p> | |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title', 'caspar' ); ?> | |
:</label><br/> | |
<input type="text" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" | |
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" | |
value="<?php echo esc_attr( $title ); ?>" class="widefat"/> | |
</p> | |
<hr> | |
<p> | |
<label | |
for="<?php echo esc_attr( $this->get_field_id( 'image_id' ) ); ?>"><?php _e( 'Image ID:', 'caspar' ); ?> | |
:</label> | |
<input type="text" id="<?php echo esc_attr( $this->get_field_id( 'image_id' ) ); ?>" | |
name="<?php echo esc_attr( $this->get_field_name( 'image_id' ) ); ?>" | |
value="<?php echo esc_attr( $image_id ); ?>" class="widefat"/> | |
</p> | |
<p> | |
<label for="<?php echo esc_attr( $this->get_field_id( 'caption' ) ); ?>"><?php _e( 'Caption', 'caspar' ); ?> | |
:</label><br/> | |
<input type="text" id="<?php echo esc_attr( $this->get_field_id( 'caption' ) ); ?>" | |
name="<?php echo esc_attr( $this->get_field_name( 'caption' ) ); ?>" | |
value="<?php echo esc_attr( $caption ); ?>" class="widefat"/> | |
</p> | |
<p> | |
<label for="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>"><?php _e( 'Link', 'caspar' ); ?> | |
:</label><br/> | |
<input type="text" id="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>" | |
name="<?php echo esc_attr( $this->get_field_name( 'link' ) ); ?>" | |
value="<?php echo esc_attr( $link ); ?>" class="widefat"/> | |
</p> | |
<?php | |
} | |
/** | |
* Sanitize and save widget data. | |
* | |
* @param array $new_instance | |
* @param array $old_instance | |
* | |
* @return array | |
*/ | |
public function update( $new_instance, $old_instance ) { | |
$new_instance['title'] = sanitize_text_field( $new_instance['title'] ); | |
$new_instance['image_id'] = (int) $new_instance['image_id']; | |
$new_instance['caption'] = sanitize_text_field( $new_instance['caption'] ); | |
$new_instance['link'] = esc_url_raw( $new_instance['link'] ); | |
return $new_instance; | |
} | |
/** | |
* Show the widget contents on the front end. | |
* | |
* @param array $args | |
* @param array $instance | |
*/ | |
public function widget( $args, $instance ) { | |
extract( $args ); | |
extract( $instance ); | |
echo $before_widget; | |
if ( ! empty( $instance['title'] ) ) { | |
echo $before_title . $instance['title'] . $after_title; | |
} | |
printf( '<p><a href="%1$s">%2$s</a><br>%3$s</p>', | |
esc_url( $link ), | |
wp_get_attachment_image( (int) $image_id, 'medium' ), | |
esc_attr( $caption ) | |
); | |
echo $after_widget; | |
} | |
} | |
add_action( 'widgets_init', __NAMESPACE__ . '\\widget_init' ); | |
/** | |
* Initialize the widget. | |
*/ | |
function widget_init() { | |
register_widget( 'Caspar\Widget_Demo' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment