【问题标题】:Wordpress settings page not saving settings to databaseWordpress 设置页面未将设置保存到数据库
【发布时间】:2011-01-31 22:51:05
【问题描述】:

我正在开发我的第一个 Wordpress 插件。我一直在遵循一些有关创建设置页面的指南。

我有以下页面,它正确显示了数据库中字段的值。当我转到该页面时,编辑字段并按“保存更改”,更改不会保存到数据库中。如果我直接在数据库中更改值,那么这些值确实会正确显示在输入字段中,但我仍然无法从我的页面更新值。

您能看出我犯了什么明显的错误或遗漏了什么吗?

<?php
add_action('admin_menu', 'SetupPage');

function SetupPage()
{   
    add_action('admin_init', 'RegisterSettings');

    // Setup administration menu item
    if (function_exists('add_options_page'))
    {
        add_menu_page(__("TestPage"), __("TestPage"), "manage_options", __FILE__, 'PageContent', plugins_url('/images/icon.png', __FILE__));
    }
}

function RegisterSettings()
{
    // Add options to database if they don't already exist
    add_option("test_option1", "", "", "yes");
    add_option("test_option2", "", "", "yes");
    add_option("test_option3", "", "", "yes");

    // Register settings that this form is allowed to update
    register_setting('test_settings', 'test_option1');
    register_setting('test_settings', 'test_option2');
    register_setting('test_settings', 'test_option3');
}

?>

<?php
function PageContent()
{
    if (!current_user_can('manage_options'))
        wp_die(__("You don't have access to this page"));

    ?>
    <div class="wrap">
    <h2><?_e("Test settings")?></h2>

    <form method="post">

        <?php settings_fields('test_settings'); ?>

        <table class="form-table">
            <tr valign="top">
            <th scope="row">test_option1</th>
            <td><input type="text" name="test_option1" value="<?php echo get_option('test_option1'); ?>" /></td>
            </tr>

            <tr valign="top">
            <th scope="row">test_option2</th>
            <td><input type="text" name="test_option2" value="<?php echo get_option('test_option2'); ?>" /></td>
            </tr>

            <tr valign="top">
            <th scope="row">test_option3</th>
            <td><input type="text" name="test_option3" value="<?php echo get_option('test_option3'); ?>" /></td>
            </tr>
        </table>

        <p class="submit">
        <input type="submit" class="button-primary" value="<?php _e('Save changes') ?>" />
        </p>

    </form>
    </div>
<?php
}
?>

【问题讨论】:

  • Otto 的教程会让你走上正轨:ottopress.com/2009/wordpress-settings-api-tutorial
  • 我尝试重新开始并按照 Otto 的教程进行操作。现在它正在工作。虽然它没有使用完全相同的方法,但我想我会继续这样做。谢谢

标签: wordpress


【解决方案1】:

在我看来,您需要在表单标签中添加action="options.php"。否则,它似乎是正确的。毫无疑问,您已经看过 this codex page,因为您的代码非常相似,但这是我看到的唯一区别。

【讨论】:

  • 我尝试过使用表单标签中的操作,但没有任何区别。
  • 为我带来了不同。谢谢朋友。
【解决方案2】:

还值得注意的是,如果您遇到此问题,请检查您是否尝试回显设置字段。

所以对我来说,我有这个:

echo '
    <div class="wrap">
        <h1>Theme Settings  <img src="' . get_stylesheet_directory_uri('stylesheet_directory') . '/images/site-icon.png" width="32" height="32" /></h1>
        <form method="post" action="options.php">
        ' . settings_fields( 'custom-settings-group' ) . '
        ' . do_settings_sections( 'custom-settings-group' ) . '
        <table class="form-table">
            <tr valign="top">
            <th scope="row">Brisbane Hours</th>
            <td><textarea rows="4" cols="40" name="brisbane_hours">' . esc_attr( get_option('brisbane_hours') ) . '</textarea></td>
            </tr>
            <tr valign="top">
            <th scope="row">Adelaide Hours</th>
            <td><input type="text" value="' . esc_attr( get_option('adelaide_hours') ) . '"/></td>
            </tr>
        </table>
        ' . submit_button() . '
    </form>
    </div>
    ';

这意味着这两个字段得到了回应:

settings_fields( 'custom-settings-group' )
do_settings_sections( 'custom-settings-group' )

改成这个为我解决了这个问题。

<?php settings_fields( 'snowys-custom-settings-group' ); ?>
<?php do_settings_sections( 'snowys-custom-settings-group' ); ?>

【讨论】:

    【解决方案3】:

    我检查了代码并从我这边进行了测试,我做了一些更改,查看了完整的工作代码

    <?php
    /**
     * Plugin Name: Testing Plugin
     */
    add_action('admin_menu', 'SetupPage');
    add_action('admin_init', 'RegisterSettings');
    
    function SetupPage() {
        add_menu_page(__("TestPage"), __("TestPage"), "manage_options", __FILE__, 'PageContent', plugins_url('/images/icon.png', __FILE__));
    }
    
    function RegisterSettings() {
        // Add options to database if they don't already exist
        add_option("test_option1", "", "", "yes");
        add_option("test_option2", "", "", "yes");
        add_option("test_option3", "", "", "yes");
    
        // Register settings that this form is allowed to update
        register_setting('test_settings', 'test_option1');
        register_setting('test_settings', 'test_option2');
        register_setting('test_settings', 'test_option3');
    }
    ?>
    
    <?php
    
    function PageContent() {
        if (!current_user_can('manage_options'))
            wp_die(__("You don't have access to this page"));
        ?>
        <div class="wrap">
            <h2><? _e("Test settings") ?></h2>
    
            <form method="post" action="options.php">
    
                <?php settings_fields('test_settings'); ?>
    
                <table class="form-table">
                    <tr valign="top">
                        <th scope="row">test_option1</th>
                        <td><input type="text" name="test_option1" value="<?php echo get_option('test_option1'); ?>" /></td>
                    </tr>
    
                    <tr valign="top">
                        <th scope="row">test_option2</th>
                        <td><input type="text" name="test_option2" value="<?php echo get_option('test_option2'); ?>" /></td>
                    </tr>
    
                    <tr valign="top">
                        <th scope="row">test_option3</th>
                        <td><input type="text" name="test_option3" value="<?php echo get_option('test_option3'); ?>" /></td>
                    </tr>
                </table>
    
                <p class="submit">
                    <input type="submit" class="button-primary" value="<?php _e('Save changes') ?>" />
                </p>
    
            </form>
        </div>
        <?php
    }
    ?>
    

    希望对某人有所帮助:)

    【讨论】:

      【解决方案4】:

      您使用的是哪种浏览器?听起来可能很奇怪,但是使用 Chrome 时,我发现了一些无法正确保存设置的插件。

      不是一个很好的技术答案,但如果该插件仅供您自己使用,并且您可以使其管理功能在 Firefox 和 IE 中运行,则可能更容易满足于“足够好”。

      【讨论】:

        【解决方案5】:

        您有多站点集吗?如果是这样,该设置将在本地 wp_*_options 单站点表的 wp_options 表中创建。你必须使用

        add_blog_option( get_current_blog_id(), "option_name", "" );
        

        当您在现有博客上强制使用多站点并且现在尝试管理选项时,就会发生这种情况。

        【讨论】:

          猜你喜欢
          • 2019-01-21
          • 1970-01-01
          • 2015-12-05
          • 2010-11-26
          • 1970-01-01
          • 1970-01-01
          • 2014-05-13
          • 1970-01-01
          • 2018-04-03
          相关资源
          最近更新 更多