add_action( \'admin_init\', \'register_mysettings\' );
function register_mysettings() {
//register our settings
register_setting( \'plugin_options\', \'plugin_options\', \'plugin_options_validate\' );
add_settings_section(\'plugin_main\', \'\', \'plugin_section_text\', \'plugin\');
add_settings_field(\'plugin_text_string\', \'\', \'plugin_setting_string\', \'plugin\',
\'plugin_main\');
}
function poly_admin_form() {
?>
<div class="wrap">
<h2>Change who the email and name is from</h2>
<?php if ($_REQUEST[\'settings-updated\'] == \'true\' ) : ?>
<div id="message" class="updated fade"><p><strong><?php _e( \'Options saved\' ); ?></strong></p></div>
<?php endif; // If the form has just been submitted, this shows the notification ?>
<form method="post" action="options.php">
<?php settings_fields(\'plugin_options\'); ?>
<?php do_settings_sections(\'plugin\'); ?>
<?php submit_button(); ?>
</form>
</div>
<?php }
function plugin_section_text() {
echo \'\';
}
function plugin_setting_string() {
$options = get_option(\'plugin_options\');
echo "
<tr valign=\'top\'>
<th scope=\'row\'>Admin Email</th>
<td><input type=\'text\' id=\'plugin_text_string\' name=\'plugin_options[text_string]\' value=\'{$options[\'text_string\']}\' /></td>
</tr>
<tr valign=\'top\'>
<th scope=\'row\'>Website Name or Person Name</th>
<td><input type=\'text\' id=\'plugin_text_string\' name=\'plugin_options[text_string_name]\' value=\'{$options[\'text_string_name\']}\' /></td>
</tr>
";
}
// validate our options
function plugin_options_validate($input) {
$options = get_option(\'plugin_options\');
$options[\'text_string\'] = trim($input[\'text_string\']);
$options[\'text_string_name\'] = trim($input[\'text_string_name\']);
if(empty($options)) {
$options[\'text_string\'] = \'[email protected]\';
$options[\'text_string_name\'] = \'Polyindustry\';
}
return $options;
}
//Sends bulk email
function bulk_mail(){
poly_subscription_activation();
add_filter(\'wp_mail_from\', \'new_mail_from\');
add_filter(\'wp_mail_from_name\', \'new_mail_from_name\');
function new_mail_from($old) {
return $options[\'text_string\'];
}
function new_mail_from_name($old) {
return $options[\'text_string_name\'];
}
最合适的回答,由SO网友:Charles Clarkson 整理而成
在plugin_options_validate()
函数返回值get_option( \'plugin_options\' )
从未使用过。
让我们通过添加一些注释逐步完成该函数:
// Retrieve the options.
$options = get_option(\'plugin_options\');
// Replace the options without checking if
// trim( $input[\'text_string\'] ) and trim( $input[\'text_string_name\'] )
// actually return strings and if $input[\'text_string\'] and
// $input[\'text_string_name\'] have anything in them.
$options[\'text_string\'] = trim($input[\'text_string\']);
$options[\'text_string_name\'] = trim($input[\'text_string_name\']);
// This part never runs. `$options` will never be empty!
if ( empty( $options ) ) {
$options[\'text_string\'] = \'[email protected]\';
$options[\'text_string_name\'] = \'Polyindustry\';
}
实际上,此函数与您编写的函数相同:
// Validate options.
function plugin_options_validate( $input ) {
$options[\'text_string\'] = trim( $input[\'text_string\'] );
$options[\'text_string_name\'] = trim( $input[\'text_string_name\'] );
return $options;
}