如何让Wordpress小部件“记住”值

时间:2010-01-03 01:43:44

标签: php wordpress widget

我正在制作我的第一个 Wordpress 小部件 - 只需加载 jQuery tweet

所以我的问题是管理界面中的小部件编辑器不会“记住”我的值,我确定它只是与变量有关,而不是传递给_control函数。

那么有人会有什么建议吗?

这是我的代码:

  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
 }

1 个答案:

答案 0 :(得分:0)

你正在使用哪个版本的wordpress ...如果它的2.8或更高版本,那么我强烈建议使用新的wordpress widget api ...这是一个面向对象的api非常容易使用,肯定会解决你的问题。 ...

结帐: New Widget API