复选框默认未选中

时间:2013-10-18 02:49:39

标签: php wordpress checkbox

对于我的Wordpress插件选项页面,我想使用复选框来设置选项。我正在使用此代码将其添加到选项页面,它运行良好。除了我希望复选框默认设置为未选中。我该怎么做?

public function id_number_callback()
{
printf(
'<input id="%1$s" name="cus_func_option[%1$s]" type="checkbox" %2$s" />',
'add_image_dimesions',
checked( isset( $this->options['add_image_dimesions'] ), true, false )
);

编辑:我想要使用的完整代码是

class MySettingsPage
{
/**
 * Holds the values to be used in the fields callbacks 
 */
private $options;

/**
 * Start up
 */
public function __construct()
{
    add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
    add_action( 'admin_init', array( $this, 'page_init' ) );
}

/**
 * Add options page
 */
public function add_plugin_page()
{
    // This page will be under "Settings"
    add_options_page(
        'Settings Admin', 
        'Custom functions', 
        'manage_options', 
        'cus-func-admin', 
        array( $this, 'create_admin_page' )
    );
}

/**
 * Options page callback
 */
public function create_admin_page()
{
    // Set class property
    $this->options = get_option( 'cus_func_option' );
?>
<div class="wrap">
    <?php screen_icon(); ?>
    <h2>Custom Functions</h2>           
    <form method="post" action="options.php">
    <?php
        // This prints out all hidden setting fields
        settings_fields( 'cus_func_group' );   
        do_settings_sections( 'cus-func-admin' );
        submit_button(); 
    ?>
    </form>
</div>
<?php
}

/**
 * Register and add settings
 */
public function page_init()
{        
    register_setting(
        'cus_func_group', // Option group
        'cus_func_option', // Option name
        array( $this, 'sanitize' ) // Sanitize
    );

    add_settings_section(
        'setting_section_id', // ID
        'Which Custom Functions do you want to use?', // Title
        array( $this, 'print_section_info' ), // Callback
        'cus-func-admin' // Page
    );  

    add_settings_field(
        'add_image_dimesions', // ID
        'Add image dimesions to the Media Page', // Title 
        array( $this, 'id_number_callback' ), // Callback
        'cus-func-admin', // Page
        'setting_section_id' // Section           
    );      

    add_settings_field(
        'title', 
        'Title', 
        array( $this, 'title_callback' ), 
        'cus-func-admin', 
        'setting_section_id'
    );      
}

/**
 * Sanitize each setting field as needed
 *
 * @param array $input Contains all settings fields as array keys
 */
public function sanitize( $input )
{
    $new_input = array();
    if( isset( $input['add_image_dimesions'] ) )
        $new_input['add_image_dimesions'] = absint( $input['add_image_dimesions'] );

    if( isset( $input['title'] ) )
        $new_input['title'] = sanitize_text_field( $input['title'] );

    return $new_input;
}

/** 
 * Print the Section text
 */
public function print_section_info()
{
    print 'Pick your Custom Functions below:';
}

/** 
 * Get the settings option array and print one of its values
 */
public function id_number_callback()
{


    printf(
        '<input id="%1$s" name="cus_func_option[%1$s]" type="checkbox" %2$s" />',
'add_image_dimesions',
checked( isset( $this->options['add_image_dimesions'] ), true, false )
);

}

/** 
 * Get the settings option array and print one of its values
 */
public function title_callback()
{


printf(
        '<input id="%1$s" name="cus_func_option[%1$s]" type="checkbox" %2$s />',
'title',
checked( isset( $this->options['title'] ), true, false )
);  


}
}

if( is_admin() )
$my_settings_page = new MySettingsPage();

2 个答案:

答案 0 :(得分:0)

请参阅WordPress Documentation for checked

代码中isset( $this->options['add_image_dimesions'] )的价值是多少?

这是决定是否检查的原因。

答案 1 :(得分:0)

如果您希望在加载页面时取消选中始终复选框,则可以删除第二个printf标记%2$2,并将第二个参数删除到printf,{{ 1}},它会给你这个:

checked( isset( $this->options['add_image_dimesions'] ), true, false )
相关问题