Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
    }
}

?>
share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

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 );
}
share|improve this answer
    
Magnificent, thanks solved! –  user2619611 Sep 5 '13 at 16:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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