2010-09-03 72 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); 
      } 
); 
}