我正在为我的插件创建一些选项/选项页面。在本文的上下文中,我只添加了两个复选框来存储布尔值。创建、在页面上显示和保存效果良好。关于未来的选项,我想测试选项的有效性。为此,我创建了函数;“清理\\u选项”;,它现在只返回作为测试接收的参数。
public function sanitize_options( $data ) {
return $data;
}
我将函数名传递给函数;register\\u settings“注册设置”;作为参数。
register_setting(
\'faqdesk_general_options\', //Group Name
\'faqdesk_general_options\', //Name of the option
array(
\'type\' => \'array\',
\'sanitize_callback\' => \'sanitize_options\',
)
);
当然,这没有多大意义,也没有任何作用。但正如我所说,我只是想尝试一下。但是现在我不得不注意到这个函数根本没有被调用。此外,当我在页面上保存更改时,选项值填充为NULL。如果我将值sanitize\\u回调完全从参数数组中取出,那么一切都会正常工作,因此不会出现错误。
这是什么原因,错误在哪里?我是在犯愚蠢的思维错误吗?请帮帮我,如果我只是个傻瓜,请原谅我。
完成课程后:
<?php
class Faqdesk_Settings {
/**
* Single instance of the class
*
* @var null
* @since 1.0.0
*/
protected static $_instance = null;
/**
* Singleton
*
* @since 1.0.0
* @static
* @return self Main instance
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Add admin menu.
*
* @return void
*/
public function settings_admin_menu() {
add_submenu_page(
\'faqdesk_main_menu\', //slug name for the parent menu (menu_slug is set add_admin_menu() function in the class Faqdesk_Admin)
__( \'FAQDesk Settings\', \'faqdesk\' ), //page title shown in the <title> tag
__( \'Settings\', \'faqdesk\' ), //name of the submenu displayed on the Dashboard
\'manage_options\', //minimum capability required to view the submenu
\'faqdesk_settings\', //slug name of the submenu
array($this,\'render_settings_page_content\') //function to be called to display the page content
);
}
/**
* Renders a page with a tab navigation to display the settings.
*/
public function render_settings_page_content( $active_tab = \'\' ) {
?>
<div class="wrap">
<h2><?php _e( \'FAQDesk Settings\', \'faqdesk\' ); ?></h2>
<?php settings_errors(); ?>
<?php if( isset( $_GET[ \'tab\' ] ) ) {
$active_tab = $_GET[ \'tab\' ];
} else if( $active_tab == \'social_options\' ) {
$active_tab = \'social_options\';
} else if( $active_tab == \'input_examples\' ) {
$active_tab = \'input_examples\';
} else {
$active_tab = \'general_options\';
} // end if/else ?>
<h2 class="nav-tab-wrapper">
<a href="?page=faqdesk_settings&tab=general_options" class="nav-tab <?php echo $active_tab == \'general_options\' ? \'nav-tab-active\' : \'\'; ?>"><?php _e( \'General\', \'faqdesk\' ); ?></a>
</h2>
<form method="post" action="options.php">
<?php
settings_fields( \'faqdesk_general_options\' ); //settings group name - call references the whitelisted option which had declared with register_setting()
do_settings_sections( \'faqdesk_settings\' ); //slug name of the page whos settings section should be outputed, therefore it outputs all the sections and form fields which has defined in initialize_general_options()
submit_button(__( \'Save general settings\', \'faqdesk\' ), \'\');
?>
</form>
</div><!-- /.wrap -->
<?php
}
/**
* This function provides a simple description for the General Options page.
*
* It\'s called from the \'initialize_general_options\' function by being passed as a parameter
* in the add_settings_section function.
*/
public function general_options_callback() {
$options = get_option(\'faqdesk_general_options\');
var_dump($options);
echo \'<p>\' . __( \'Adjust the settings, which concern basic details and functions of the plugin.\', \'faqdesk\' ) . \'</p>\';
} // end general_options_callback
/**
* Initializes the general options page by registering the Sections,
* Fields, and Settings.
*
* This function is registered with the \'admin_init\' hook.
*/
public function initialize_general_options() {
// Register setting: The group name can be anything actually, but I think its just simpler to name it the same as the option that will get stored in the database.
register_setting(
\'faqdesk_general_options\', //Group Name
\'faqdesk_general_options\', //Name of the option
array(
\'type\' => \'array\',
\'sanitize_callback\' => \'sanitize_options\',
)
);
// Add section, which defines how the settings will be visually grouped
add_settings_section(
\'general_settings_section\', // also HTML ID tag for the section
__( \'General Options\', \'faqdesk\' ), // Title that will show within an <H2> tag
array( $this, \'general_options_callback\'), // Callback that will echo some explanations about that section
\'faqdesk_settings\' // slug name of the page which to show the section
);
// Add settings fields, describes how to add the form input
// SETTING: Cleanup on deactivation?
add_settings_field(
\'delete_data_on_deactivation\', // also HTML ID tag
__( \'Cleanup on deactivation\', \'faqdesk\' ), // Formatted title of the field, which is displayed as the label for the field on output
array( $this, \'toggle_delete_data_on_deactivation_callback\'), // Callback function that will echo the form field
\'faqdesk_settings\', // The page on which this option will be displayed
\'general_settings_section\', // The name of the section to which this field belongs
array( // The array of arguments to pass to the callback. In this case, just a description.
__( \'Activate this setting to delete all data, when the plugin is deactivated.\', \'faqdesk\' )
)
);
// SETTING: Cleanup on deletion?
add_settings_field(
\'delete_data_on_deletion\', // also HTML ID tag
__( \'Cleanup on deletion\', \'faqdesk\' ), // Formatted title of the field, which is displayed as the label for the field on output
array( $this, \'toggle_delete_data_on_deletion_callback\'), // Callback function that will echo the form field
\'faqdesk_settings\', // The page on which this option will be displayed
\'general_settings_section\', // The name of the section to which this field belongs
array( // The array of arguments to pass to the callback. In this case, just a description.
__( \'Activate this setting to delete all data, when the plugin is deleted.\', \'faqdesk\' )
)
);
} // end wppb-demo_initialize_theme_options
/**
* This function renders the interface elements for toggling the visibility of the header element.
*
* It accepts an array or arguments and expects the first element in the array to be the description
* to be displayed next to the checkbox.
*/
public function toggle_delete_data_on_deactivation_callback($args) {
// First, we read the options collection
$options = get_option(\'faqdesk_general_options\');
// Next, we update the name attribute to access this element\'s ID in the context of the display options array
// We also access the show_header element of the options collection in the call to the checked() helper function
$html = \'<input type="checkbox" id="delete_data_on_deactivation" name="faqdesk_general_options[delete_data_on_deactivation]" value="1" \' . checked( 1, isset( $options[\'delete_data_on_deactivation\'] ) ? $options[\'delete_data_on_deactivation\'] : 0, false ) . \'/>\';
// Here, we\'ll take the first argument of the array and add it to a label next to the checkbox
$html .= \'<label for="delete_data_on_deactivation"> \' . $args[0] . \'</label>\';
echo $html;
}
public function toggle_delete_data_on_deletion_callback($args) {
// First, we read the options collection
$options = get_option(\'faqdesk_general_options\');
$delete_on_deactivation = isset( $options[\'delete_data_on_deactivation\'] ) ? $options[\'delete_data_on_deactivation\'] : 0;
$attribute = \'\';
if($delete_on_deactivation == 1) {
$attribute = \' disabled="disabled" faqdesk-tooltip="\' . __( \'Not so useful to set this when already at deactivation all plugin data is deleted...\', \'faqdesk\' ) . \'" \';
}
// Next, we update the name attribute to access this element\'s ID in the context of the display options array
// We also access the show_header element of the options collection in the call to the checked() helper function
$html = \'<input type="checkbox"\' . $attribute . \'id="delete_data_on_deletion" name="faqdesk_general_options[delete_data_on_deletion]" value="1" \' . checked( 1, isset( $options[\'delete_data_on_deletion\'] ) ? $options[\'delete_data_on_deletion\'] : 0, false ) . \'/>\';
// Here, we\'ll take the first argument of the array and add it to a label next to the checkbox
$html .= \'<label for="delete_data_on_deletion"> \' . $args[0] . \'</label>\';
echo $html;
}
public function sanitize_options( $data ) {
return $data;
}
}