首先:我真的是个新手。因此,我为我的选项页面使用了一个模板,并尝试对其进行自定义(到目前为止效果很好)。
关于我的插件:它创建发布类型和其中的一些元框。元框有一些字段,到目前为止保存字段的效果相当不错。
现在我要说的是,我希望我的选项不仅显示在选项页面上,而且保存输入的值。
我用“简短摘录”试试,因为所有其他选项都是一样的。
<?php
add_action( \'admin_menu\', \'my_admin_menu\' );
function my_admin_menu() {
add_options_page( __(\'Nutri+\', \'nutriplus\' ), __(\'Nutri+\', \'nutriplus\' ), \'manage_options\', \'my-plugin\', \'my_options_page\' );
}
add_action( \'admin_init\', \'my_admin_init\' );
function my_admin_init() {
register_setting( \'my-settings-group\', \'my-plugin-settings\' );
add_settings_section( \'section-1\', __( \'General Settings\', \'nutriplus\' ), \'section_1_callback\', \'my-plugin\' );
add_settings_field( \'field-1-1\', __( \'Enable Food Post Type\', \'nutriplus\' ), \'field_1_1_callback\', \'my-plugin\', \'section-1\' );
add_settings_field( \'field-1-2\', __( \'- Enable Nutritional Information Meta Box\', \'nutriplus\' ), \'field_1_2_callback\', \'my-plugin\', \'section-1\' );
add_settings_field( \'field-1-3\', __( \'- Enable Additional Information Meta Box\', \'nutriplus\' ), \'field_1_3_callback\', \'my-plugin\', \'section-1\' );
add_settings_field( \'field-1-4\', __( \'Enable Diet Post Type\', \'nutriplus\' ), \'field_1_4_callback\', \'my-plugin\', \'section-1\' );
function my_options_page() {
?>
<div class="wrap">
<h2><?php _e(\'Nutri+ Options\', \'nutriplus\'); ?></h2>
<form action="options.php" method="POST" class="np-options">
<?php settings_fields(\'my-settings-group\'); ?>
<?php do_settings_sections(\'my-plugin\'); ?>
<?php submit_button(); ?>
</form>
</div>
<?php }
function section_1_callback() {
_e( \'Here you can configure Nutri+ on a per post type base and enable/disable features.\', \'nutriplus\' );
}
function field_1_1_callback() {
$settings = (array) get_option( \'my-plugin-settings\' );
$field = "field_1_1";
$value = esc_attr( $settings[$field] );
echo "<input id=\'enable_food_post_type\' name=\'my-plugin-settings[$field]\' type=\'checkbox\' value=\'true\' checked />";
}
function field_1_2_callback() {
$settings = (array) get_option( \'my-plugin-settings\' );
$field = "field_1_2";
$value = esc_attr( $settings[$field] );
echo "<input id=\'enable_nutition_meta_box\' name=\'my-plugin-settings[$field]\' type=\'checkbox\' value=\'true\' checked />";
}
function field_1_3_callback() {
$settings = (array) get_option( \'my-plugin-settings\' );
$field = "field_1_3";
$value = esc_attr( $settings[$field] );
echo "<input id=\'enable_additional_meta_box\' name=\'my-plugin-settings[$field]\' type=\'checkbox\' value=\'true\' checked />";
}
function field_1_4_callback() {
$settings = (array) get_option( \'my-plugin-settings\' );
$field = "field_1_4";
$value = esc_attr( $settings[$field] );
echo "<input id=\'enable_diet_post_type\' name=\'my-plugin-settings[$field]\' type=\'checkbox\' value=\'true\' checked />";
}
function my_settings_validate_and_sanitize( $input ) {
$settings = (array) get_option( \'my-plugin-settings\' );
if ( $some_condition == $input[\'field_1_1\'] ) {
$output[\'field_1_1\'] = $input[\'field_1_1\'];
} else {
add_settings_error( \'my-plugin-settings\', \'invalid-field_1_1\', __(\'Error in Field "Enable Food Post Type"\', \'nutriplus\'), \'error\' );
}
if ( $some_condition == $input[\'field_1_2\'] ) {
$output[\'field_1_2\'] = $input[\'field_1_2\'];
} else {
add_settings_error( \'my-plugin-settings\', \'invalid-field_1_2\', __(\'Error in Field "Enable Nutritional Information Meta Box"\', \'nutriplus\'), \'error\' );
}
if ( $some_condition == $input[\'field_1_3\'] ) {
$output[\'field_1_3\'] = $input[\'field_1_3\'];
} else {
add_settings_error( \'my-plugin-settings\', \'invalid-field_1_3\', __(\'Error in Field "Enable Additional Information Meta Box"\', \'nutriplus\'), \'error\' );
}
if ( $some_condition == $input[\'field_1_4\'] ) {
$output[\'field_1_4\'] = $input[\'field_1_4\'];
} else {
add_settings_error( \'my-plugin-settings\', \'invalid-field_1_4\', __(\'Error in Field "Enable Diet Post Type"\', \'nutriplus\'), \'error\' );
}
return $output;
}
我不知道复选框是否需要清理或验证…我不知道如何保存当前值,当然也不知道如何在默认情况下加载。我找到了使用php函数而不是value=“xxx”的示例,但不知道如何将其包括在内,是的……我知道……当需要显示当前值时,checked属性不应该存在:)也许有人可以帮我