it-swarm.cn

帮助自定义小部件选项! (保存复选框选项)

我为Wordpress创建了一个自定义的Flickr Widget并成功设置了一个选项表单供用户输入他们的Flickr信息,但我无法获取表单中的复选框以保存是否已选中。任何帮助将不胜感激!这是我的widget(),form()和update()函数:

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); ?>

任何帮助将不胜感激!!!

1
Christian Benincasa

我会做:

function update($new_instance, $old_instance) {
    ...

    $instance['show_photostream'] = $new_instance['show_photostream'];

    return $instance;
}

function form($instance) {
    $defaults = array (
        ...
        'show_photostream' => !empty( $instance['show_photostream'] ) ? $instance['show_photostream'] : false
    );
    $instance = wp_parse_args( (array) $instance, $defaults);
1
sorich87