Widget default options not saving
-
Hi, I wrote my first plugin/widget yesterday and I’m trying to figure out why it won’t use the default setting for the number of posts that should be displayed if the user does not fill in a value. When the user enters a value, it adheres to it, but when the value is empty, it’s not using the default value of 3. Can anyone help? Here’s my code:
function soup() { ?> <p> <?php $options = get_option('widget_soup'); if (!is_array( $options )) { $options = array( 'soup_number' => '3' ); } $soupnumber = $options['soup_number']; $my_query = new WP_Query(array('posts_per_page' => $soupnumber, 'nopaging' => 0, 'post_status' => 'future', 'order' => 'ASC')); if ($my_query->have_posts()) { while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?> <ul> <li> <?php the_title(); ?> </li> </ul> <?php endwhile; } ?> </p> <p> <a href="<?php bloginfo('rss2_url'); ?>" title="Subscribe to <?php bloginfo('name'); ?>"> <img style="vertical-align:middle; margin:0 10px 0 0;" src="<?php bloginfo('wpurl'); ?>/wp-content/plugins/soup-upcoming-post/icons/rss.png" width="16px" alt="Subscribe to <?php bloginfo('name'); ?>" /> </a> Don't miss it - <strong><a href="<?php bloginfo('rss2_url'); ?>" title="Subscribe to <?php bloginfo('name'); ?>">Subscribe by RSS.</a></strong> </p> <p> Or, just <strong><a href="https://eepurl.com/bz-Mz" title="Subscribe to Do More With WordPress">subscribe to the newsletter!</a></strong> </p> <?php } function widget_soup($args) { extract($args); $options = get_option("widget_soup"); if (!is_array( $options )) { $options = array( 'title' => 'Upcoming Posts' ); } echo $before_widget; echo $before_title; echo $options['title']; echo $after_title; soup(); echo $after_widget; } function soup_control() { $options = get_option('widget_soup'); if (!is_array( $options )) { $options = array( 'title' => 'Upcoming Posts', 'soup_number' => 3 ); } if ($_POST['soup-submit']) { $options['title'] = htmlspecialchars($_POST['soup_widget_title']); $options['soup_number'] = htmlspecialchars($_POST['soup_no_of_posts']); update_option("widget_soup", $options); } ?> <p> <label for="soup_widget_title">Widget title: </label> <input type="text" id="soup_widget_title" name="soup_widget_title" value="<?php echo $options['title'];?>" /> </p> <p> <label for="soup_no_of_posts">No. of posts: </label> <input type="text" id="soup_no_of_posts" name="soup_no_of_posts" value="<?php echo $options['soup_number'];?>" /> <input type="hidden" id="soup-submit" name="soup-submit" value="1" /> </p> <?php } function soup_init() { register_sidebar_widget (__('Upcoming Posts'), 'widget_soup'); register_widget_control( 'Upcoming Posts', 'soup_control' ); } add_action('plugins_loaded', 'soup_init'); ?>
- The topic ‘Widget default options not saving’ is closed to new replies.