如何在wp_options表中保存自定义表单数据

时间:2021-02-10 作者:DK Thakur

我有一些数据要存储在wp\\U选项表中。

我知道代码不对,但我想以这种方式存储数据:

global $wpdb;

if (isset($_POST[\'add_estimated_satisfaction\'])) {
    // run validation if you\'re not doing it in js
    $option_name = $confused_about_treatment_editor;
    $option_name = $estimated_price_editor;
    $option_name = $satisfaction_guarantee_editor;

    $option_value = isset($_POST[\'confused_about_treatment\']) ? $_POST[\'confused_about_treatment\'] : \'\';
    $option_value = isset($_POST[\'estimated_price\']) ? $_POST[\'estimated_price\'] : \'\';
    $option_value = isset($_POST[\'satisfaction_guarantee\']) ? $_POST[\'satisfaction_guarantee\'] : \'\';


    $status = $wpdb->insert(\'www_options\', array(
        \'option_name\'  => $option_name,
        \'option_value\' => $option_value,

    ));

    if ($status == false) {
        echo $msg_fail;
    } else {
        echo $msg_success_save;
    };
    go_back();
}
?>
<form method="POST">
    <table width="95%">
        <tr>
            <td width="10%">Confused about our treatment</td>
            <td width="90%">
                <?php
                $confused_about_treatment_content  = \'\';
                $confused_about_treatment_editor   = "confused_about_treatment";
                $confused_about_treatment_settings = array(
                    \'textarea_name\' => $confused_about_treatment_editor,
                    \'textarea_rows\' => 15,
                    \'media_buttons\' => false,
                    \'teeny\'         => false,
                    \'tinymce\'       => true,
                    \'quicktags\'     => false,
                );
                ?>
                <textarea style="display:none" name="<?php $confused_about_treatment_editor ?>" id="<?php $confused_about_treatment_editor ?>" rows="10" cols="100"><?php echo $confused_about_treatment_content ?></textarea>
                <?php wp_editor($confused_about_treatment_content, $confused_about_treatment_editor,
                    $confused_about_treatment_settings); ?>

            </td>
        </tr>

        <tr>
            <td width="10%">Tooltip Estimated Price</td>
            <td width="90%">
                <?php
                $estimated_price_content  = \'\';
                $estimated_price_editor   = "estimated_price";
                $estimated_price_settings = array(
                    \'textarea_name\' => $estimated_price_editor,
                    \'textarea_rows\' => 5,
                    \'media_buttons\' => false,
                    \'teeny\'         => false,
                    \'tinymce\'       => false,
                    \'quicktags\'     => false,
                );
                ?>
                <textarea style="display:none" name="<?php $estimated_price_editor ?>" id="<?php $estimated_price_editor ?>" rows="10" cols="100"><?php echo $estimated_price_content ?></textarea>
                <?php wp_editor($estimated_price_content, $estimated_price_editor, $estimated_price_settings); ?>

            </td>
        </tr>

        <tr>
            <td width="10%">Tooltip Satisfaction Guarantee</td>
            <td width="90%">
                <?php
                $satisfaction_guarantee_content  = \'\';
                $satisfaction_guarantee_editor   = "satisfaction_guarantee";
                $satisfaction_guarantee_settings = array(
                    \'textarea_name\' => $satisfaction_guarantee_editor,
                    \'textarea_rows\' => 5,
                    \'media_buttons\' => false,
                    \'teeny\'         => false,
                    \'tinymce\'       => false,
                    \'quicktags\'     => false,
                );
                ?>
                <textarea style="display:none" name="<?php $satisfaction_guarantee_editor ?>" id="<?php $satisfaction_guarantee_editor ?>" rows="10" cols="100"><?php echo $satisfaction_guarantee_content ?></textarea>
                <?php wp_editor($satisfaction_guarantee_content, $satisfaction_guarantee_editor,
                    $satisfaction_guarantee_settings); ?>

            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="add_estimated_satisfaction" value="Submit"/></td>
        </tr>

    </table>
</form>

1 个回复
最合适的回答,由SO网友:Daniel Morell 整理而成

@如果您能够使用Buttered\\u Toast的解决方案,那么它就是您想要的。

你的代码有很多问题。

变量$confused_about_treatment_editor, $estimated_price_editor, 和$satisfaction_guarantee_editor 从未实例化$option_name 和$option_value更改三次,但只使用最后一个值go_back() 每一次,但它每次都被调用。

我建议使用update_option() 用于在站点运行期间更改的表单值。对于不希望变异的值,应使用add_option().

您可以尝试删除// run validation comment, 和$status 使用此。。。

$status = update_option(\'form_values\', [
    \'confused_about_treatment\' => isset( $_POST[\'confused_about_treatment\'] ) ? $_POST[\'confused_about_treatment\'] : \'\',
    \'estimated_price\' => isset( $_POST[\'estimated_price\'] ) ? $_POST[\'estimated_price\'] : \'\',
    \'satisfaction_guarantee\' => isset( $_POST[\'satisfaction_guarantee\'] ) ? $_POST[\'satisfaction_guarantee\'] : \'\',
]);

相关推荐