使用Settings API 显示字段和Options API 存储价值将是一个很好的方法。
您的直觉是正确的——创建一个自定义表来存储API键不是一种可行的方法。
Edit
这里有一个完整的例子
https://wpengineer.com/2139/adding-settings-to-an-existing-page-using-the-settings-api/) 将自定义设置添加到“选项-常规”页面:
// Register and define the settings
add_action(\'admin_init\', \'ozhwpe_admin_init\');
function ozhwpe_admin_init(){
register_setting(
\'general\', // settings page
\'ozhwpe_options\', // option name
\'ozhwpe_validate_options\' // validation callback
);
add_settings_field(
\'ozhwpe_notify_boss\', // id
\'Boss Email\', // setting title
\'ozhwpe_setting_input\', // display callback
\'general\', // settings page
\'default\' // settings section
);
}
// Display and fill the form field
function ozhwpe_setting_input() {
// get option \'boss_email\' value from the database
$options = get_option( \'ozhwpe_options\' );
$value = $options[\'boss_email\'];
// echo the field
?>
<input id=\'boss_email\' name=\'ozhwpe_options[boss_email]\'
type=\'text\' value=\'<?php echo esc_attr( $value ); ?>\' /> Boss wants to get a mail when a post is published
<?php
}
// Validate user input
function ozhwpe_validate_options( $input ) {
$valid = array();
$valid[\'boss_email\'] = sanitize_email( $input[\'boss_email\'] );
// Something dirty entered? Warn user.
if( $valid[\'boss_email\'] != $input[\'boss_email\'] ) {
add_settings_error(
\'ozhwpe_boss_email\', // setting title
\'ozhwpe_texterror\', // error ID
\'Invalid email, please fix\', // error message
\'error\' // type of message
);
}
return $valid;
}
我更改了代码,使该选项显示在“常规选项”页面上。修改此代码以使用API密钥字段不会太困难。