2010-09-03 66 views
2

我爲Wordpress創建了一個自定義Flickr小部件,併成功地爲用戶輸入了他們的Flickr信息設置了一個選項表單,但是我無法獲取表單中的複選框以保存是否被選中。任何幫助將不勝感激!這裏是我的部件(),窗體()和更新()的功能:保存wordpress小部件問題中的複選框值

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

    $title = apply_filters('widget_title', $instance['title']); 
    $displaynum = $instance['displaynum']; 
    $flickrid = $instance['flickrname']; 
    $num = 6; 
    $feed = new SimplePie($instance['feed']); 
    $feed->handle_content_type(); 
    $photostream = $instance['show_photostream']; 


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

    $instance['title'] = strip_tags($new_instance['title']); 
    $instance['displaynum'] = strip_tags($new_instance['displaynum']); 
    $instance['feed'] = $new_instance['feed']; 
    $instance['flickrname'] = $new_instance['flickrname']; 
    $instance['show_photostream'] = (bool) $new_instance['show_photostream']; 

    return $instance; 
} 

function form($instance) { 
    $defaults = array (
     'title' => 'My Recent Flickr Uploads', 
     'displaynum' => 6, 
     'feed' => 'http://api.flickr.com/services/feeds/[email protected]&lang=en-us&format=rss_200', 
     'flickrname' => 'rastajellyfish', 
     'show_photostream' => isset($instance['show_photostream']) ? (bool) $instance['show_photostream'] : false 
    ); 
    $instance = wp_parse_args((array) $instance, $defaults); ?> 

任何幫助將不勝感激!

+0

你給了複選框一個名字?我知道未選中的複選框不會作爲_POST/_GET中的變量傳遞。 – Steven 2010-09-03 20:16:16

+0

它確實分配了一個名稱。 – 2010-09-05 18:52:31

回答

2

我建議你檢查位於默認的部件:wp-includes/default-widgets.php

3

5分鐘前,我遇到了同樣的問題,因爲你在我的自定義圖像控件複選框,並期待通過默認控件作爲Hnrch建議解決了它。

我的問題是由於在我的複選框輸入上回顯錯誤的名稱屬性造成的。有一種名爲get_field_name()的方法會輸出正確的輸入名稱。這是我的(現在的功能)複選框的例子:

<input id="<?php echo $this->get_field_id('linktarget'); ?>" name="<?php echo $this->get_field_name('linktarget'); ?>" type="checkbox" <?php checked(isset($instance['linktarget']) ? $instance['linktarget'] : 0); ?> />&nbsp; 

「linktarget」應該是「show_photostream」你的情況

1

不正是你要找的,但是這是我在metabox保存複選框,你可以從中得到一些暗示...

代碼用來顯示HTML

function html_rtsocial_metabox() 
{ 
    global $post; 

    $custom = get_post_custom($post->ID); 
    $suppress = $custom['_suppress_rtsocial'][0]; 
    $checked = ($suppress=='yes')?'checked':''; 
    $value = ($suppress=='yes')?'yes':'no'; 
    $html = '<br><input type="checkbox" name="_suppress_rtsocial" value="'.$value.'" '.$checked.' id="_suppress_rtsocial" /> Hide Social Icons ???'; 
    $html .= '<br/><br/>'; 
    echo $html; 
} 

同時節約

update_post_meta($post_id,'_suppress_rtsocial',$_POST['_suppress_rtsocial']); 

加js的管理界面

function checkbox_helper(){ 
    var ele =jQuery('#_suppress_rtsocial'),value; 
    ele.click(function(){ 
       value = ele.is(':checked')?'yes':'no'; 
       ele.val(value); 
      } 
); 
}