【发布时间】:2012-08-14 19:31:14
【问题描述】:
首先,我想先为自己的天真或愚蠢行为道歉。我是一个新手程序员,但我正在努力学习。现在,我正在尝试为 wordpress 开发一个插件,作为我实习的一部分。该插件在很大程度上完成了它需要做的事情,但我似乎无法让设置页面按我想要的方式工作。我知道插件的初始化部分工作正常,但我在使用设置页面时遇到了问题。具体来说,我不知道在哪里或如何使用 _POST 来保存信息。从设置页面收到信息后,我也没有将 update_option 函数调用放在哪里。这是我现在的代码:
add_action( 'admin_menu', 'menu' );
add_action( 'admin_init', 'plugin_admin_init' );
function plugin_admin_init() { // whitelist options
register_setting( 'plugin_options', 'plugin_options', 'plugin_options_validate' );
add_settings_section('plugin_main', 'Main Settings', 'plugin_section_text', 'plugin');
add_settings_field('plugin_text_string', 'REE View Count Settings', 'plugin_setting_string', 'plugin', 'plugin_main');
}
function menu() {
add_options_page( 'REE View Count Options', 'REE View Count', 'manage_options', 'plugin', 'plugin_options_page' );
}
// generates the settings web page
function plugin_options_page() {
echo '<div class="wrap">';
echo '<h2>Options</h2>';
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
// In the line above, I don't know what to put. The file name of the plugin?
// The URL of the settings page on the admin page? Or what I have currently?
// Both the first and last options bring up a 404 page.
settings_fields('plugin_options');
do_settings_sections('plugin');
echo '<input name="Submit" type="submit" value="' . esc_attr_e('Save Changes') . '" />';
echo '</form>';
//update_option('plugin_options', $_POST['plugin_options']);
// Where should I try to update the option? And how?
echo '</div>';
}
//from add_settings_section parameter 3, in plugin_admin_init
function plugin_section_text() {
echo '<p>Main description of this section here.</p>';
}
//Function from 3 paramter of add_settings_field in plugin_admin_init that outputs input HTML
function plugin_setting_string() {
$options = get_option('plugin_options');
echo 'value of options is: ' . $options;
echo "<input id='plugin_text_string' name='plugin_options' size='40' type='text'
value='{$options}' />";
}
//Validation function from register_setting in plugin_admin_init
function plugin_options_validate($input) {
echo "this is input: " . $input;
$newinput = trim($input);
if(!preg_match('/^[0-9]+$/', $newinput)) {
$newinput = 10;
}
update_option('plugin_options', $newinput);
return $newinput;
}
注意:我实际上并没有使用这些确切的函数/变量名称 - 我在它们前面加上实际的插件名称。
你能帮帮我吗?我想将管理员输入的信息带入设置页面并使用该信息更新数据库(在本例中只是一个数字)。我知道我需要使用 PHP _POST,但我不知道如何。此外,我没有发帖到哪里,因为当我使用 action="the_file_name.php" 时,我在提交时收到 404 错误。表单的操作应该是什么,以便我可以使用从管理员提交中获得的信息并将其用于以后?在我这样做之后,如何更新设置?以及如何,我的意思是我在哪里放置update_option?如果这看起来漫不经心或含糊不清,我深表歉意 - 我觉得我有点过头了。
如果有帮助,我会在本指南的帮助下,在与插件本身相同的文件中构建此设置页面:http://ottopress.com/2009/wordpress-settings-api-tutorial/
不幸的是,我在该指南中没有看到任何关于更新信息的内容,而只是创建页面本身。
【问题讨论】: