如果第三方插件(您要扩展的插件)使用设置API,如果您现在打开由第三方插件定义的选项页面和选项组,则可以添加新的设置字段。也只需使用设置:
首先add_setting_field()
您可以向第三个插件定义的任何设置部分添加新字段第二,带register_setting()
您可以在插件定义的选项组中注册新设置一个非常简单的例子:
add_action( \'admin_init\', \'cyb_add_settings_field_to_plugin\' );
function cyb_add_settings_field_to_plugin() {
add_settings_field(
\'some_id\',
\'Some title\',
\'cyb_field_callback\',
\'plugin-settins-page\', // Settings page defined by the third party plugin
\'plugin-settings-section\', // Section defined by the third party plugin
array()
);
register_setting(
\'option-group\', // Options group defined by third party plugin
\'my-option-name\', // Custom option name
\'cyb_sanitize_callback\' // Sanitize
);
}
function cyb_field_callback() {
$value = get_option( \'my-option-name\' );
?>
<input type="text" id="some_id" name="my-option-name" value="<?php echo esc_attr( $value ); ?>" />
<?php
}
function cyb_sanitize_callback( $inputs ) {
// Do sanitization of the the inputs
return $inputs;
}
如果愿意,还可以添加新的部分:
add_action( \'admin_init\', \'cyb_add_settings_field_to_plugin\' );
function cyb_add_settings_field_to_plugin() {
add_settings_section(
\'new-settings-section\',
\'Settings Section Title\',
\'cyb_print_section_info\', // Callback
\'plugin-settins-page\' // Settings page defined by the third party plugin
);
add_settings_field(
\'some_id\',
\'Some title\',
\'cyb_field_callback\',
\'plugin-settins-page\', // Settings page defined by the third party plugin
\'new-settings-section\', // My custom section defined above
array()
);
register_setting(
\'option-group\', // Options group defined by third party plugin
\'my-option-name\', // Option name
\'cyb_sanitize_callback\' // Sanitize
);
}
function cyb_print_section_info() {
echo \'Section info\';
}
function cyb_field_callback() {
$value = get_option( \'my-option-name\' );
?>
<input type="text" id="some_id" name="my-option-name" value="<?php echo esc_attr( $value ); ?>" />
<?php
}
function cyb_sanitize_callback( $inputs ) {
// Do sanitization of the the inputs
return $inputs;
}