Skip to content

Instantly share code, notes, and snippets.

@jimmy89Li
Created July 26, 2016 08:01
Show Gist options
  • Save jimmy89Li/e09f71c5ab40297c461c3e266c725ad9 to your computer and use it in GitHub Desktop.
Save jimmy89Li/e09f71c5ab40297c461c3e266c725ad9 to your computer and use it in GitHub Desktop.
Custom Text Widget
<?php
/*
Plugin Name: Custom Text Widget
Plugin URI: https://gist.github.com/jimmy89Li
Description: Custom Text Widget with an Image from url, Title and Content
Author: jimmyLi
Version: 1.0.0
Author URI: https://liviusimion.com
*/
class CustomTextWidget extends WP_Widget
{
function CustomTextWidget()
{
$widget_ops = array(
'classname' => 'custom-text-widget',
'description' => 'Custom Text Widget' );
$this->WP_Widget('CustomTextWidget', 'Custom Text Widget', $widget_ops);
}
function widget($args, $instance)
{
extract($args, EXTR_SKIP);
?>
<?php echo $before_widget; ?>
<img src="<?php echo ($instance['customImage']) ?>" alt="<?php echo ($instance['customTitle']) ?>" class="alignkeft" width="50" height="50"/>
<h3><?php echo ($instance['customTitle']); ?></h3>
<p><?php echo ($instance['customContent']); ?></p>
<?php echo $after_widget;?>
<?php
}
function form($instance)
{
?>
<label for="<?php echo $this->get_field_id('customImage'); ?>">
Image url:
<input class="widefat" type="url"
id="<?php echo $this->get_field_id('customImage'); ?>"
name="<?php echo $this->get_field_name('customImage'); ?>"
value="<?php echo esc_attr($instance['customImage']); ?>" />
</label>
<label for="<?php echo $this->get_field_id('customTitle'); ?>">
Title:
<input class="widefat" type="text"
id="<?php echo $this->get_field_id('customTitle'); ?>"
name="<?php echo $this->get_field_name('customTitle'); ?>"
value="<?php echo esc_attr($instance['customTitle']); ?>" />
</label>
<label for="<?php echo $this->get_field_id('customContent'); ?>">
Content:
<textarea class="widefat" type="text" rows="4"
id="<?php echo $this->get_field_id('customContent'); ?>"
name="<?php echo $this->get_field_name('customContent'); ?>"><?php echo esc_attr($instance['customContent']); ?></textarea>
</label>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['customImage'] = ( !empty($new_instance['customImage']) ? strip_tags( $new_instance['customImage']) : '' );
$instance['customTitle'] = ( !empty($new_instance['customTitle']) ? $new_instance['customTitle'] : '' );
$instance['customContent'] = ( !empty($new_instance['customContent']) ? $new_instance['customContent'] : '' );
do_action( 'wp_editor_widget_update', $new_instance, $instance );
return apply_filters( 'wp_editor_widget_update_instance', $instance, $new_instance );
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("CustomTextWidget");') );?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment