0

我有以下代码,我想根据 checkbox=checked 调用代码底部的函数。

我试过用

if (get_option('add_image_dimensions') === '1') { ...}

但这没有用,尽管我希望它可以工作。如何让这个东西工作?

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 = array(
        'add_image_dimensions'  => '0', 
        'title'             => '0'  

    );
    $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_dimensions', // ID
        'Add image dimensions 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_dimensions'] ) )
        $new_input['add_image_dimensions'] = absint( $input['add_image_dimensions'] );

    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_dimensions',
checked( isset( $this->options['add_image_dimensions'] ), 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();



if (get_option('add_image_dimensions') === '1') { 
// ADD IMAGE DIMENSIONS TO MEDIA PAGE
add_filter('manage_upload_columns', 'size_column_register');
function size_column_register($columns) {
$columns['dimensions'] = 'Dimensions';
return $columns;
}
add_action('manage_media_custom_column', 'size_column_display', 10, 2);
function size_column_display($column_name, $post_id) {
if( 'dimensions' != $column_name || !wp_attachment_is_image($post_id))
return;
list($url, $width, $height) = wp_get_attachment_image_src($post_id, 'full');
echo esc_html("{$width}&times;{$height}");
}
}
4

1 回答 1

1

您没有正确清理这些值。absint用于数值,sanitize_text_field用于文本字段。你有复选框,应该是

$new_input['title'] = esc_attr( $input['title'] ); // change "title" for a descriptive name

其他设置字段相同。

然后,选项存储在 name 下cus_func_option,这是一个包含其他值的数组。它应该是:

$check = get_option('cus_func_option');
# Uncomment to debug
// var_dump($check);
// die();
if( $check )
{
    if( isset( $check['add_image_dimensions']) )
    {
        add_filter('manage_upload_columns', 'size_column_register');
        add_action('manage_media_custom_column', 'size_column_display', 10, 2);
    }
}
function size_column_register() { /*etc*/ }
function size_column_display() { /*etc*/ }
于 2013-10-18T07:57:05.677 回答