Created
February 17, 2015 18:28
-
-
Save thefuxia/b318faa2b79d9bb8ffe8 to your computer and use it in GitHub Desktop.
Plugin T5 Excerpt Widget.
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 # -*- coding: utf-8 -*- | |
/** | |
* Plugin Name: T5 Excerpt Widget | |
* Plugin URI: https://gist.github.com/toscho/b318faa2b79d9bb8ffe8 | |
* Description: Shows the post excerpt in a widget. The widget is visible on single posts only. | |
* Version: 17.02.15 | |
* Required: 4.0 | |
* Author: Thomas Scholz <info@toscho.de> | |
* Author URI: http://toscho.de | |
* License: MIT | |
* License URI: http://www.opensource.org/licenses/mit-license.php | |
*/ | |
class T5_Excerpt_Widget extends WP_Widget { | |
public function __construct() { | |
parent::__construct( 't5_excerpt_widget', __( 'Excerpt' ) ); | |
} | |
public function form( $instance ) { | |
$instance = wp_parse_args( $instance, array( 'title' => '' ) ); | |
print $this->get_title( $instance[ 'title' ] ); | |
} | |
private function get_title( $title ) | |
{ | |
$title = esc_attr( $title ); | |
$id = $this->get_field_id( 'title' ); | |
$name = $this->get_field_name( 'title' ); | |
$label = __( 'Title:' ); | |
return "<p> | |
<label for='$id'>$label | |
<input class='widefat' id='$id' name='$name' value='$title' type='text'> | |
</label> | |
</p>"; | |
} | |
public function update( $new_instance, $old_instance ) { | |
$new_instance[ 'title' ] = esc_attr( $new_instance[ 'title' ] ); | |
return $new_instance; | |
} | |
public function widget( $args, $instance ) { | |
if ( ! is_single() ) | |
return; | |
$excerpt = get_post()->post_excerpt; | |
if ( '' === $excerpt ) | |
return; | |
$title = ''; | |
if ( ! empty ( $instance['title' ] ) ) | |
$title = $args['before_title'] . $instance['title'] . $args['after_title']; | |
print $args['before_widget'] . $title; | |
print wpautop( $excerpt ); | |
print $args['after_widget']; | |
} | |
public static function register() { | |
register_widget( __CLASS__ ); | |
} | |
} | |
add_action( 'widgets_init', array ( 'T5_Excerpt_Widget', 'register' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment