Created
May 15, 2017 08:21
-
-
Save carlodaniele/4c318326d26867a16d876cb2e6a2a441 to your computer and use it in GitHub Desktop.
This is an example plugin for Kinsta blog readers
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 | |
/** | |
* @package Kinsta_widget | |
* @version 1.0 | |
*/ | |
/* | |
Plugin Name: Kinsta Widget | |
Plugin URI: http://wordpress.org/extend/plugins/# | |
Description: This is an example plugin | |
Author: Your Name | |
Version: 1.0 | |
Author URI: http://example.com/ | |
*/ | |
class Kinsta_Widget extends WP_Widget { | |
/** | |
* Sets up the widgets name etc | |
* | |
* @link https://developer.wordpress.org/reference/classes/wp_widget/__construct/ | |
* @see https://developer.wordpress.org/reference/functions/wp_register_sidebar_widget/ | |
* | |
*/ | |
public function __construct() { | |
$widget_ops = array( | |
'classname' => 'kinsta_widget', | |
'description' => 'A plugin for Kinsta blog readers', | |
); | |
parent::__construct( 'kinsta_widget', 'Kinsta Widget', $widget_ops ); | |
} | |
/** | |
* Outputs the content of the widget on front-end | |
* | |
* @param array $args Widget arguments | |
* @param array $instance | |
* | |
* @link https://developer.wordpress.org/reference/classes/wp_widget/widget/ | |
*/ | |
public function widget( $args, $instance ) { | |
echo $args['before_widget']; | |
if ( ! empty( $instance['title'] ) ) { | |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; | |
} | |
if( ! empty( $instance['selected_posts'] ) && is_array( $instance['selected_posts'] ) ){ | |
$selected_posts = get_posts( array( 'post__in' => $instance['selected_posts'] ) ); | |
?> | |
<ul> | |
<?php foreach ( $selected_posts as $post ) { ?> | |
<li><a href="<?php echo get_permalink( $post->ID ); ?>"><?php echo $post->post_title; ?></a></li> | |
<?php } ?> | |
</ul> | |
<?php | |
}else{ | |
echo esc_html__( 'No posts selected!', 'text_domain' ); | |
} | |
echo $args['after_widget']; | |
} | |
/** | |
* Outputs the options form on admin | |
* | |
* @param array $instance The widget options | |
* | |
* @link https://developer.wordpress.org/reference/classes/wp_widget/form/ | |
*/ | |
public function form( $instance ) { | |
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'Title', 'text_domain' ); | |
?> | |
<p> | |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label> | |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"> | |
</p> | |
<p> | |
<?php | |
$posts = get_posts( array( | |
'posts_per_page' => 20, | |
'offset' => 0 | |
) ); | |
$selected_posts = ! empty( $instance['selected_posts'] ) ? $instance['selected_posts'] : array(); | |
?> | |
<div style="max-height: 120px; overflow: auto;"> | |
<ul> | |
<?php foreach ( $posts as $post ) { ?> | |
<li><input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'selected_posts' ) ); ?>[]" value="<?php echo $post->ID; ?>" <?php checked( ( in_array( $post->ID, $selected_posts ) ) ? $post->ID : '', $post->ID ); ?> /><?php echo get_the_title( $post->ID ); ?></li> | |
<?php } ?> | |
</ul> | |
</div> | |
<?php | |
} | |
/** | |
* Processing widget options on save | |
* | |
* @param array $new_instance The new options | |
* @param array $old_instance The previous options | |
* | |
* @link https://developer.wordpress.org/reference/classes/wp_widget/update/ | |
*/ | |
public function update( $new_instance, $old_instance ) { | |
$instance = array(); | |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; | |
$selected_posts = ( ! empty ( $new_instance['selected_posts'] ) ) ? (array) $new_instance['selected_posts'] : array(); | |
$instance['selected_posts'] = array_map( 'sanitize_text_field', $selected_posts ); | |
return $instance; | |
} | |
} | |
// register Kinsta_Widget | |
add_action( 'widgets_init', function(){ | |
register_widget( 'Kinsta_Widget' ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment