0

对于我的 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();
4

2 回答 2

0

如果您希望在加载页面时始终取消选中该复选框,那么您只需删除第二个 printf 标记%2$2和 printf 的第二个参数checked( isset( $this->options['add_image_dimesions'] ), true, false ),这将为您提供:

public function id_number_callback()
{
printf(
'<input id="%1$s" name="cus_func_option[%1$s]" type="checkbox" />',
'add_image_dimesions'
);
于 2013-10-18T03:01:23.923 回答
0

请参阅WordPress 文档进行检查

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

这就是确定是否检查的原因。

于 2013-10-18T02:52:20.957 回答