使用复选框选项在wordpress中构建插件

时间:2013-11-20 11:48:44

标签: wordpress plugins checkbox

我正在尝试使用一些复选框构建一个小部件,但我不知道如何保存值。 有没有办法保存他们我几乎在完成我的小部件结束时,谢谢。 这部分代码:

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

        <a id="facebook" target="_blank" href="<?php echo $instance['urlfacebook'] ?>"></a>
        <a id="twitter" target="_blank" href="<?php echo $instance['urltwitter'] ?>"></a>
        <a id="picassa" target="_blank" href="<?php echo $instance['urlpicassa'] ?>"></a>
        <a id="youtube" target="_blank" href="<?php echo $instance['urlyoutube'] ?>"></a>
        <a id="rss" target="_blank" href="<?php echo $instance['urlrss'] ?>"></a>

    <?php    
    }

1 个答案:

答案 0 :(得分:0)

这会对你有所帮助。保存wordpress选项的基本示例。

<?php
// create custom plugin settings menu
add_action('admin_menu', 'baw_create_menu');

function baw_create_menu() {

    //create new top-level menu
    add_menu_page('BAW Plugin Settings', 'BAW Settings', 'administrator', __FILE__, 'baw_settings_page',plugins_url('/images/icon.png', __FILE__));

    //call register settings function
    add_action( 'admin_init', 'register_mysettings' );
}


function register_mysettings() {
    //register our settings
    register_setting( 'baw-settings-group', 'new_option_name' );
    register_setting( 'baw-settings-group', 'some_other_option' );
    register_setting( 'baw-settings-group', 'option_etc' );
}

function baw_settings_page() {
?>
<div class="wrap">
<h2>Your Plugin Name</h2>

<form method="post" action="options.php">
    <?php settings_fields( 'baw-settings-group' ); ?>
    <?php do_settings_sections( 'baw-settings-group' ); ?>
    <table class="form-table">
        <tr valign="top">
        <th scope="row">New Option Name</th>
        <td><input type="text" name="new_option_name" value="<?php echo get_option('new_option_name'); ?>" /></td>
        </tr>

        <tr valign="top">
        <th scope="row">Some Other Option</th>
        <td><input type="text" name="some_other_option" value="<?php echo get_option('some_other_option'); ?>" /></td>
        </tr>

        <tr valign="top">
        <th scope="row">Options, Etc.</th>
        <td><input type="text" name="option_etc" value="<?php echo get_option('option_etc'); ?>" /></td>
        </tr>
    </table>

    <?php submit_button(); ?>

</form>
</div>
<?php } ?>

来源:http://codex.wordpress.org/Creating_Options_Pages