在WordPress的自定义PHP插件中保存设置

时间:2012-08-14 19:31:14

标签: php post wordpress-plugin

首先,我想为做任何天真或愚蠢的事情先发制人道歉。我是一名新手程序员,但我正在努力学习。现在,我正在尝试为我的实习开发一个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/

不幸的是,我在该指南中没有看到任何有关更新信息的内容,只是创建页面本身。

1 个答案:

答案 0 :(得分:0)

你想指向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/