如何使用wordpress保存小部件中的复选框

时间:2013-11-21 09:25:21

标签: wordpress checkbox widget

我正在尝试从小部件中的不同复选框保存值,但我不能,我是新建的插件和小部件,我感到有点沮丧,因为以正常的方式很容易做

我的一些代码:

function form($instance){

    $default = array(
        'title' => 'iconos sociales', 
        'urlsprite' => 'urlsprite',            
        'urlfacebook' => 'urlfacebook', 
        'urltwitter' => 'urltwitter', 
        'urlpicassa' => 'urlpicassa', 
        'urlyoutube' => 'urlyoutube', 
        'urlrss' => 'urlrss',
        'checkboxSocial' => 'checkboxSocial',             
        'chkfacebook' => 'chkfacebook', 
        'chktwitter' => 'chktwitter', 
        'chkpicassa' => 'chkpicassa', 
        'chkyoutube' => 'chkyoutube', 
        'chkrss' => 'chkrss'
    );
    $instance = wp_parse_args((array) $instance, $default); // si no tenia un valor predeterminado le ponemos uno
    $title = esc_attr($instance['title']);
    $urlsprite= esc_attr($instance['urlsprite']);        
    $urlfacebook= esc_attr($instance['urlfacebook']);
    $urltwitter= esc_attr($instance['urltwitter']);
    $urlpicassa= esc_attr($instance['urlpicassa']);
    $urlyoutube= esc_attr($instance['urlyoutube']);
    $urlrss= esc_attr($instance['urlrss']);
    $checkboxSocial = $instance["checkboxSocial"];        
    $chkfacebook = $instance["chkfacebook"];
    $chktwitter = $instance["chktwitter"];
    $chkpicassa = $instance["chkpicassa"];
    $chkyoutube = $instance["chkyoutube"];
    $chkrss = $instance["chkrss"];
    ?>

    <p>Title <input class="widefat" name="<?php echo $this -> get_field_name('title'); ?>" type="text" value="<?php echo $title ?>" /></p>
    <br />
    Seleciona las diferentes redes
    <br />
    <label>Url sprite:</label>
    <input type="text" name="<?php echo $this -> get_field_name('urlsprite'); ?>" id="<?php echo $this -> get_field_id('urlsprite'); ?>" />
    <br />
    <input type="checkbox" name="<?php echo $this -> get_field_name('checkboxSocial'); ?>" id="<?php echo $this -> get_field_id('chkfacebook'); ?>" value="<?php echo $this -> get_field_name('chkfacebook'); ?>" <?php echo checked('1', $checkboxSocial); ?>/>
    <label>Facbook</label>
    <input type="text" name="<?php echo $this -> get_field_name('urlfacebook'); ?>" id="<?php echo $this -> get_field_id('urlfacebook'); ?>" />
    <br />
    <input type="checkbox" name="<?php echo $this -> get_field_name('checkboxSocial'); ?>" id="<?php echo $this -> get_field_id('chktwitter'); ?>" value="<?php echo $this -> get_field_name('chktwitter'); ?>" <?php echo checked('1', $checkboxSocial); ?>/>
    <label>Twitter</label>
    <input type="text" name="<?php echo $this -> get_field_name('urltwitter'); ?>" id="<?php echo $this -> get_field_id('urltwitter'); ?>" />
    <br />
    <input type="checkbox" name="<?php echo $this -> get_field_name('checkboxSocial'); ?>" id="<?php echo $this -> get_field_id('chkpicassa'); ?>" value="<?php echo $this -> get_field_name('chkpicassa'); ?>" <?php echo checked('1', $checkboxSocial); ?>/>
    <label>Picassa</label>
    <input type="text" name="<?php echo $this -> get_field_name('urlpicassa'); ?>" id="<?php echo $this -> get_field_id('urlpicassa'); ?>" />
    <br />
    <input type="checkbox" name="<?php echo $this -> get_field_name('checkboxSocial'); ?>" id="<?php echo $this -> get_field_id('chkyoutube'); ?>" value="<?php echo $this -> get_field_name('chkyoutube'); ?>" <?php echo checked('1', $checkboxSocial); ?>/>
    <label>Youtube</label>
    <input type="text" name="<?php echo $this -> get_field_name('urlyoutube'); ?>" id="<?php echo $this -> get_field_id('urlyoutube'); ?>" />
    <br />
    <input type="checkbox" name="<?php echo $this -> get_field_name('checkboxSocial'); ?>" id="<?php echo $this -> get_field_id('chkrss'); ?>" value="<?php echo $this -> get_field_name('chkrss'); ?>" <?php echo checked('1', $checkboxSocial); ?>/>
    <label>Rss</label>
    <input type="text" name="<?php echo $this -> get_field_name('urlrss'); ?>" id="<?php echo $this -> get_field_id('urlrss'); ?>" />
<?php
}


/*
 * metodo save form
 */
function update($new_instance, $old_instance){
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']); // new_instance es el titulo que ingresa el usuario
    $urlsprite = strip_tags($new_instance['urlsprite']);
    $urlfacebook = strip_tags($new_instance['urlfacebook']);        
    $urltwitter = strip_tags($new_instance['urltwitter']);
    $urlpicassa = strip_tags($new_instance['urlpicassa']);        
    $urlyoutube = strip_tags($new_instance['urlyoutube']);
    $urlrss = strip_tags($new_instance['urlrss']);               
    $chkfacebook = $new_instance['chkfacebook'];
    $chktwitter = $new_instance['chktwitter'];
    $chkpicassa = $new_instance['chkpicassa'];
    $chkyoutube = $new_instance['chkyoutube'];
    $chkrss = $new_instance['chkrss'];

    return $instance;
}   
function widget($args, $instance){          
extract($args);
    if ($instance['chkfacebook'] == 'true'){

        echo '<a id="facebook" target="_blank" href="'.$instance["urlfacebook"].'"></a>';   
    }
    if ($instance['chktwitter'] == 'true'){

        echo '<a id="facebook" target="_blank" href="'.$instance["urltwitter"].'"></a>';
    }
    if ($instance['chkpicassa'] == 'true'){

        echo '<a id="facebook" target="_blank" href="'.$instance["urlpicassa"].'"></a>';
    }
    if ($instance['chkyoutube'] == 'true'){

        echo '<a id="facebook" target="_blank" href="'.$instance["urlyoutube"].'"></a>';
    }
    if ($instance['chkrss'] == 'true'){

        echo '<a id="facebook" target="_blank" href="'.$instance["urlrss"].'"></a>';
    }   
}

0 个答案:

没有答案