Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function bg_image_plugin_loaded() {
- if(class_exists('Simple_Image_Widget')){
- add_action( 'widgets_init', 'bg_image_widgets_init' );
- } else {
- add_action('admin_notices', 'bg_image_notices');
- }
- }
- add_action('plugins_loaded', 'bg_image_plugin_loaded');
- function bg_image_widgets_init() {
- class BG_Image_Widget extends Simple_Image_Widget {
- function __construct() {
- $widget_options = array( 'classname' => 'widget_bg_image', 'description' => 'A widget to add images as decorative background patterns.' );
- parent::__construct( 'bg-image', 'BG Image Widget', $widget_options );
- add_action('simple_image_widget_field-height', array($this, 'bg_field_height'), 10, 2);
- add_filter('simple_image_widget_instance', array($this, 'bg_update'), 10, 4);
- //add_filter('simple_image_widget_output', 'bg_image_widget_output');
- }
- function form_fields(){
- return array('height');
- }
- function bg_field_height($instance) {
- ?>
- <p>
- <label for="<?php echo $this->get_field_id( 'height' ); ?>"><?php _e( 'Height:', 'bg-image-widget' ); ?></label>
- <input type="text" name="<?php echo $this->get_field_name( 'height' ); ?>" id="<?php echo $this->get_field_id( 'height' ); ?>" value="<?php echo esc_attr( $instance['height'] ); ?>" class="widefat">
- </p>
- <?php
- }
- function bg_update($instance, $new_instance, $old_instance, $id_base) {
- // @todo Sanitize custom values.
- return $instance;
- }
- function render($args, $instance){
- $src = wp_get_attachment_image_src( $instance['image_id'], 'full');
- if ( ! empty( $instance['image_id'] ) ) {
- $output = '<div class="pattern-widget" id="bg-'.urlencode($instance['title']).'" style="background-image:url('.$src[0].'); height:'.$instance['height'].'px;">';
- $output .= '</div><!---->';
- }else{
- $output = '<div class="hidden error"><p>There is no image for this, please pick an image first!</p></div>';
- }
- return $output;
- }
- }
- // Register the widget.
- register_widget( 'BG_Image_Widget' );
- }
- function bg_image_notices(){
- global $pagenow;
- if($pagenow == 'widgets.php'){
- echo '<div class="updated">No Simple Image Widget Class for BG Image Widget to Use</div>';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment