views:

87

answers:

1

Hello! I am in the process of making my first Wordpress widget, just one that loads in jQuery tweet. So my problem is that the widget editor in the admin screen won't "remember" my values, I'm sure it just has something to do with the variables just not passing onto the _control function. I am new with PHP so would anybody have any advice? Here's my code:

  function widget_qTweet($args) {
  extract($args);
  $options = get_option('widget_qTweet');
  $title = empty($options['title']) ? 'Tweet, Tweet!' : $options['title'];
  $total = empty($options['total']) ? '3' : $options['total'];
  $username = empty($options['username']) ? 'kylehotchkiss' : $options['username'];

  // Here's our "Tweet"
  echo $before_widget;
  echo $before_title . $title . $after_title; ?>
  <script type="text/javascript">
   jQuery(document).ready(function($) {
    $(".tweets").tweet({
     username:"<?php echo $username; ?>",
     count: <?php echo $total; ?>,
     loading_text:"Loading Tweets..."
    });
   });
  </script>
  <div class="tweets"></div>
  <?php echo $after_widget;
 }

 function widget_qTweet_control() {
  $options = get_option('widget_qTweet');
  if ( $_POST['qTweet-submit'] ) {
   $newoptions['title'] = strip_tags(stripslashes($_POST['qTweet-title']));
   $newoptions['total'] = strip_tags(stripslashes($_POST['qTweet-total']));
   $newoptions['username'] = strip_tags(stripslashes($_POST['qTweet-username']));
  }

  if ( $options != $newoptions ) {
   $options = $newoptions;
   update_option('widget_qTweet', $options);
  }

  $title = htmlspecialchars($options['title'], ENT_QUOTES);
  $total = htmlspecialchars($options['total'], ENT_QUOTES);
  $username = htmlspecialchars($options['username'], ENT_QUOTES);
  ?>
<div>
 <p>
  <label for="qTweet-title" style="display:block;">Widget Title:</label>
  <input type="text" id="qTweet-title" name="qTweet-title" value="<?php echo $title; ?>" />
 </p>
 <p>
  <label for="qTweet-username" style="display:block;">Your Twitter Username:</label>
  <input type="text" id="qTweet-username" name="qTweet-username" value="<?php echo $username; ?>" />
 </p>
 <p>
  <label for="qTweet-total" style="display:block;">Number of Tweets:</label>
  <select id="qTweet-total" name="qTweet-total">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
   <option value="4">4</option>
   <option value="5">5</option>
  </select>
 </p>
 <input type="hidden" name="qTweet-submit" id="qTweet-submit" value="1" />
</div>
  <?php
 }
A: 

which version of wordpress are you using... if its 2.8 or later then i will strongly recommend using the new wordpress widget api... It is an object oriented api very easy to work with and will definitely solve your problem....

checkout: New Widget API

Pragati Sureka