【问题标题】:Save WordPress plugin settings with form使用表单保存 WordPress 插件设置
【发布时间】:2016-10-25 06:13:11
【问题描述】:

您好,我有这个 wordpress 项目,我的脚本运行良好并将其保存到数据库中。我的问题是当我插入值 zipcode 2034 并刷新 wp-admin 页面时,我认为数据正在保存,现在我想输入另一个 zipcode 6000 并且第一个输入值被新的值替换。我怎么能从中插入另一个值?不替换保存的数据。下面是我的代码

add_action('admin_menu', 'zipcode_menu');

function zipcode_menu(){
    add_menu_page( 'Zipcode Page', 'Zipcode', 'manage_options', 'zipcode', 'zipcode' );
}

add_action( 'admin_init', 'update_extra_post_info' );

function update_extra_post_info() {
    register_setting( 'extra-post-info-settings', 'zipcode' );

}

function zipcode() {


    // Now display the settings editing screen

    echo '<div class="wrap">';
    // header
    echo "<h2>" . __( 'Zipcode', 'zip' ) . "</h2>";
    // settings form
    $extra_info = get_option('zipcode');
    echo "<pre>";
    print_r($extra_info);
    echo "</pre>";

    ?>

    <form method="post" action="options.php">
    <?php settings_fields( 'extra-post-info-settings' ); ?>
    <?php do_settings_sections( 'extra-post-info-settings' ); ?>
    <table class="form-table">
        <tr valign="top">
            <th scope="row">Zipcode:</th>
            <td><input type="text" name="zipcode" value="<?php echo get_option( 'zipcode' ); ?>"/></td>
        </tr>
    </table>
    <?php submit_button(); ?>
    </div>
    <br>
    <br>

    <table width="600px">
        <tr>
            <th>
                Zipcode
            </th>
            <th>
                Options
            </th>
        </tr>
        <tr>
            <th>

            </th>
            <th>

            </th>
        </tr>

    </table>
    <?php

}

我想从数据库中检索所有保存的数据到table 有人可以帮我解决这个问题吗?非常感谢任何帮助。 TIA

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    在 Wordpress register_setting 中在表 wp_options 中进行注册所以 它总是会取代您选择的价值。所以我建议创建一个新表并从该表中管理您的邮政编码。

    【讨论】:

    • 啊,好吧,你的意思是我会创建自己的表,然后保存我创建的数据是吗?
    • 所以在 register_setting 中总是会替换值 hmmmm
    • 是的,默认情况下 register_setting 它将数据保存到同一个表中的 option_name 中,并且您需要每个邮政编码的 id,因此您不能在 wp_options 中执行此操作
    猜你喜欢
    • 2022-08-10
    • 2015-11-08
    • 2019-07-21
    • 1970-01-01
    • 2016-03-22
    • 2015-06-29
    • 2012-06-08
    • 2014-03-31
    • 1970-01-01
    相关资源
    最近更新 更多