Skip to content

Instantly share code, notes, and snippets.

@rhinkle
Created May 13, 2015 17:22
Show Gist options
  • Select an option

  • Save rhinkle/0a567f572111fb5a2dcc to your computer and use it in GitHub Desktop.

Select an option

Save rhinkle/0a567f572111fb5a2dcc to your computer and use it in GitHub Desktop.
An WordPress widget that allows users to include an html file as an widget. (ie use for signup forms)
<?php
defined( 'WPINC' ) or die;
/**
* Adds Display_Template widget.
*/
class Display_Template extends WP_Widget {
private $template_dir;
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'include_widget', // Base ID
__( 'Display Template', 'mindutopia' ), // Name
array( 'description' => __( 'Display custom HTML template files as a widget.', 'mindutopia' ), ) // Args
);
/* Set template dir */
$this->template_dir = get_stylesheet_directory(). '/partials/display-templates';
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
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['file'] ) && $this->has_templates( $instance['file'] ) ){
ob_start();
include( $this->template_dir .'/'. $instance['file'] );
ob_end_flush();
}
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'New title', 'mindutopia' );
if ( ! empty( $instance['file'] ) && ! $this->has_templates( $instance['file'] ) ){
$file = __( 'none', 'mindutopia' );
}else{
$file = $instance['file'];
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
/* Check to see if we have templates */
if( $this->has_templates() ){
?>
<p>
<label for="<?php echo $this->get_field_id( 'file' ); ?>"><?php _e( 'Include Template:' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id( 'file' ); ?>" name="<?php echo $this->get_field_name( 'file' ); ?>">
<option value="none">Select a Template</option>
<?php
$template_array = $this->the_templates_array();
foreach ( $template_array as $template) {
?><option value="<?php echo esc_attr( $template ); ?>" <?php selected( $file, esc_attr( $template ) ); ?> ><?php echo $template; ?></option><?php
}
?>
</select>
<?php
?>
</p>
<?php
}else{
?>
<p> No templates found, please add an file to display. <small>(html files only)</small></p>
<?php
}
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['file'] = ( ! empty( $new_instance['file'] ) ) ? $new_instance['file'] : 'none';
return $instance;
}
/* ==========================================================================
Helper
========================================================================== */
/**
* Conditional helper to check if file exists or not.
*
* @param string $file The file name you would like to check.
*
* @return boolean TRUE|FALSE Status saying ether true or false
*/
private function template_exists( $file ){
if( ! $this->has_templates() ){
return false;
}
if( file_exists( $this->template_dir .'/'. $file ) ){
return true;
}else{
return false;
}
}
/**
* Conditional check to see if there are templates present.
*
* @return boolean TRUE|FALSE Status saying ether true or false
*/
private function has_templates( ){
$template_list = $this->the_templates_array();
if( count( $template_list ) <= 0 ){
return false;
}else{
return true;
}
}
/**
* Checks directory for files, then strips out any that are not `*.html`
*
* @return array $clean_dir_list An array of file names
*/
private function the_templates_array(){
$scanned_directory = scandir( $this->template_dir );
foreach ( $scanned_directory as $file ) {
if( preg_match('/(\.html)/', $file) ){
$clean_dir_list[] = $file;
}
}
return $clean_dir_list;
}
} // class Foo_Widget
// register Foo_Widget widget
function register_display_template() {
register_widget( 'Display_Template' );
}
add_action( 'widgets_init', 'register_display_template' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment