0

As the title says, i've been developing a simple image rollover widget, now that I'm almost done, the widget won't stay in the sidebar areas after I drag and drop it in and refersh the page, any idea what's going on?

<?php
/**
 * Plugin Name: Rollover Widget
 * Description: A widget that creates a rollover image effect with a hyperlink.
 * Version: 0.1
 * Author: Zak Elas
 * Author URI: http://dot.com
 */


add_action( 'widgets_init', 'my_widget' );


function my_widget() {
    register_widget( 'MY_Widget' );
}

class MY_Widget extends WP_Widget {

    function MY_Widget() {
        $widget_ops = array( 'classname' => 'example', 'description' => __('A widget that creates a rollover image effect with a hyperlink. ', 'example') );

        $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'Rollover Widget' );

        $this->WP_Widget( 'example-widget', __('Rollover Widget', 'example'), $widget_ops, $control_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );

        //Our variables from the widget settings.
        $link =  $instance['link'] ;
        $image = $instance['image'];
        $rollover_image = $instance['rollover_image'];

        echo $before_widget;

        // Display the widget link 

        echo $before_link . $link . $after_link;

        //Display the name 


         echo '<div id="test">
              <a href="'.$link.'">
              <img class="bottom" src="'.$image.'" />
              <img class="top" src="'.$rollover_image.'" />
              </a>
              </div>'; 


        echo $after_widget;
    }

    //Update the widget 

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        //Strip tags from link and name to remove HTML 
        $instance['link'] = strip_tags($new_instance['link']);
        $instance['image'] =  strip_tags($new_instance['image']);
        $instance['rollover_image'] = strip_tags( $new_instance['rollover_image']);

        return $instance;
    }


    function form( $instance ) {

        //Set up some default widget settings.
        $defaults = array( 'link' => __('Example', 'example'), 'image' => __('/images/editorial.png', 'example') , 'rollover_image' => __('/images/editorial.png', 'example') );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>


        <p>
            <label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e('link', 'example'); ?></label>
            <input id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" value="<?php echo $instance['link']; ?>" style="width:100%;" />
        </p>


        <p>
            <label for="<?php echo $this->get_field_id( 'image' ); ?>"><?php _e('image', 'example'); ?></label>
            <input id="<?php echo $this->get_field_id( 'image' ); ?>" name="<?php echo $this->get_field_name( 'image' ); ?>" value="<?php echo $instance['image']; ?>" style="width:100%;" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'rollover_image' ); ?>"><?php _e('rollover_image:', 'example'); ?></label>
            <input id="<?php echo $this->get_field_id( 'rollover_image' ); ?>" name="<?php echo $this->get_field_name( 'rollover_image' ); ?>" value="<?php echo $instance['rollover_image']; ?>" style="width:100%;" />
        </p>



    <?php
    }
}

?>
0

ID Base should be passed to WP_Widget class constructor as shown in the following code, You have passes wrong id "example-widget" which does not match with "id_base", also don't add spaces in id base. Replace MY_Widget() function with following modified function and it will work.

function MY_Widget() {
    $widget_ops = array( 'classname' => 'example', 'description' => __('A widget that creates a rollover image effect with a hyperlink. ', 'example') );

    $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'rollover-widget' );

    $this->WP_Widget( 'rollover-widget', __('Rollover Widget', 'example'), $widget_ops, $control_ops );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.