首先,我想先发制人地为做任何幼稚或愚蠢的事情道歉。我是一个新手程序员,但我正在努力学习。现在,作为实习的一部分,我正在尝试为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/
不幸的是,我在该指南中没有看到任何关于更新信息的内容,只是创建页面本身。
你想指向 action="options.php"。 它处理值的更新。 查看以下链接以获取更多详细信息。
http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/
http://www.sitepoint.com/wordpress-options-panel/