0

I am trying to create a widget for my website. This is my first instance of widget creation and I am really unsure as to what the issue is

Here is my code as it stands:

add_action( 'widgets_init', 'wy_rss_widget' );

function wy_rss_widget() {
    register_widget( 'WY_rss_widget' );
}

class WY_rss_widget extends WP_Widget {

    function WY_rss_widget() {
        $widget_ops = array( 'classname' => 'wy-rss', 'description' => __('A widget for displaying RSS feed', 'wy-rss') );
        $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'wy-rss-widget' );
        $this->WP_Widget( 'wy-rss-widget', __('RSS Widget', 'wy-rss'), $widget_ops, $control_ops );
    }

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

        //Our variables from the widget settings.
        $title = apply_filters('widget_title', $instance['title'] );
        $wy_rssURL = $instance['wy_rssURL'];
        $wy_rssLimit = $instance['wy_rssLimit'];

        echo $before_widget;

        // Display the widget title 
        if ( $title ) {
            echo $before_title . $title . $after_title;
        }

        //Display the name 
        if ( ( $wy_rssURL != " " || $wy_rssURL != "FEED URL" ) && ( $wy_rssLimit != " " ) ) {
            $rss = new DOMDocument();
            $rss->load($wy_rssURL);
            $feed = array();
            foreach ($rss->getElementsByTagName('item') as $node) {
                $item = array ( 
                    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
                    'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
                    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
                    'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
                );
                array_push($feed, $item);
            }
            $limit = $wy_rssLimit;

            echo '<ul>';
            echo '<li>'.$wy_rssURL.' ('.$limit.')</li>';
            for($x=0;$x<$limit;$x++) {
                $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
                $link = $feed[$x]['link'];
                $description = $feed[$x]['desc'];
                $date = date('l F d, Y', strtotime($feed[$x]['date']));

                echo '<li>';
                echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
                echo '<small><em>Posted on '.$date.'</em></small></p>';
                echo '<p>'.$description.'</p>';
                echo '</li>';
            }
            echo '</ul>';
        }

        echo $after_widget;
    }

    //Update the widget 

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

        //Strip tags from title and name to remove HTML 
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['wy_rssURL'] = strip_tags( $new_instance['wy_rssURL'] );
        $instance['wy_rssLimit'] = strip_tags( $new_instance['wy_rssLimit'] );

        return $instance;
    }


    function form( $instance ) {

        //Set up some default widget settings.
        $defaults = array( 'title' => __('Title', 'wy-rss'), 'wy_rssURL' => __('FEED URL', 'wy-rss'), 'wy_rssLimit' => __('5','wy-rss') );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>

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

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

        <p>
            <label for"<?php echo $this->get_field_id( 'wy_rssLimit' ); ?>"><?php _e('Number of items:', 'wp-rss'); ?></label>
            <input id="<?php echo $this->get_field_id( 'wy_rssLimit' ); ?>" name="<?php echo $this->get_field_id( 'wy_rssLimit' ); ?>" value="<?php echo $instance['wy_rssLimit']; ?>" />
        </p>

    <?php
    }
}

The value for 'wy_rssLimit' is not being stored, where the values for 'title' and 'wy_rssURL' are. I am really not sure where to look. Could someone please help?

2
  • Please don't use external sources to add code to your question. Jun 27, 2014 at 18:56
  • Many apologies. I thought it would be easier to put it onto PasteBin.
    – Nate
    Jun 28, 2014 at 21:29

1 Answer 1

1

You're using get_field_id in the name attribute of the "limit" field, where you should be using get_field_name. You're actually using get_field_name for the name attributes of the other two fields.

Try using

name="<?php echo $this->get_field_name( 'wy_rssLimit' ); ?>"

instead of

name="<?php echo $this->get_field_id( 'wy_rssLimit' ); ?>"

That should solve your issue.

The return values of the get_field_id and get_field_name methods are different: get_field_id constructs a unique ID attribute with dashes, whereas get_field_name uses square brackets to construct an array in the request when saving the widget.

1
  • Thanks! I had a feeling it would be something simple like that.
    – Nate
    Jun 28, 2014 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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