GET_OPTION返回的值与保存的值不同

时间:2013-10-24 作者:youngdeveloper
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\'];
    }
1 个回复
最合适的回答,由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;
}

结束

相关推荐

Plugins_url函数混合了系统路径和URL

在我的WordPress小部件中,我使用以下代码:wp_register_script(\'jquery-ui.widget\', plugins_url(\'assets/js/jquery-ui-1.9.2.widget.js\', dirname( __FILE__ ))); 不幸的是,代码给了我一个无效的URL,它与我的系统路径混合在一起:http://test.dev/wp-content/plugins/C:/projects/wordpress/plugins/assets/js/