AJAX重新排序表上未保存插件设置

时间:2012-05-11 作者:Zach

这是Update Option Stored in Multi-Dimensional Array 这有助于通过ajax保存可拖动的插件选项(这很有效),但不幸的是,这是特定于正确保存到数据库的实际插件设置。Ajax重新排序工作正常,但在页面保存(插件设置保存)后,这些选项将从数据库中清除。以下是设置页面:

<?php

function mouldings_options_page() {

    global $mouldings_options;

    ob_start(); ?>

    <div class="wrap">
        <h2 style="margin: 0 0 5px;"><?php _e(\'Mouldings Options\',\'roots\'); ?></h2>

        <form method="post" action="options.php" id="mouldings_options">

        <?php settings_fields(\'mouldings_settings_group\'); ?>
                <div id="thumbnail-sizes" class="wp-box">
                    <div class="wp-box-half left">
                        <div class="inner">
                            <h2><?php _e(\'Thumbnail Sizes\'); ?></h2>
                            <table class="widefat">
                                <thead>
                                    <tr>
                                        <th><?php _e(\'Post Type\',\'roots\'); ?></th>
                                        <th><?php _e(\'Width (px)\',\'roots\'); ?></th>
                                        <th><?php _e(\'Height (px)\',\'roots\'); ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><label class="description" for="mouldings_settings[idea_gallery_thumb_height]"><?php _e(\'Idea Gallery\',\'roots\'); ?></label></td>
                                        <td><input id="mouldings_settings[idea_gallery_thumb_height]" name="mouldings_settings[idea_gallery_thumb_height]" type="text" value="<?php echo (isset($mouldings_options[\'idea_gallery_thumb_height\'])) ? $mouldings_options[\'idea_gallery_thumb_height\'] : \'\'; ?>"></td>
                                        <td><input id="mouldings_settings[idea_gallery_thumb_width]" name="mouldings_settings[idea_gallery_thumb_width]" type="text" value="<?php echo (isset($mouldings_options[\'idea_gallery_thumb_width\'])) ? $mouldings_options[\'idea_gallery_thumb_width\'] : \'\'; ?>"></td>
                                    </tr>
                                    <tr>
                                        <td><label class="description" for="mouldings_settings[collection_thumb_height]"><?php _e(\'Moulding Collection\',\'roots\'); ?></label></td>
                                        <td><input id="mouldings_settings[collection_thumb_height]" name="mouldings_settings[collection_thumb_height]" type="text" value="<?php echo (isset($mouldings_options[\'collection_thumb_height\'])) ? $mouldings_options[\'collection_thumb_height\'] : \'\'; ?>"></td>
                                        <td><input id="mouldings_settings[collection_thumb_width]" name="mouldings_settings[collection_thumb_width]" type="text" value="<?php echo (isset($mouldings_options[\'collection_thumb_width\'])) ? $mouldings_options[\'collection_thumb_width\'] : \'\'; ?>"></td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="wp-box-half right">
                        <div class="inner">
                            <?php echo \'<h2>\'.__(\'Information\',\'roots\').\'</h2>\';
                            _e(\'<p>Used to resize the various images that appear throughout the Mouldings plugin. If any values are left blank, they are assumed to be un-restricted.</p>\',\'roots\'); ?>
                            <?php
                            if(function_exists(\'RegenerateThumbnails\')) {
                                echo \'<div class="m-updated"><p><a href="\'.admin_url(\'tools.php?page=regenerate-thumbnails\').\'">Regenerate Thumbnails</a> after altering these values</p></div>\';
                            }
                            else {
                                echo \'<div class="m-error"><p><strong>Notice:</strong> <a href="\'.admin_url(\'plugin-install.php?tab=search&type=term&s=regenerate+thumbnails&plugin-search-input=Search+Plugins\').\'">Regenerate Thumbnails</a> is not installed. This is required so that you can regenerate thumbnails after changing any of these values.</p></div>\';
                            }
                            ?>
                        </div>
                    </div>
                    <div class="clear"></div>
                </div>

                <div id="profile-sidebar-order" class="wp-box">
                    <div class="wp-box-half left">
                        <div class="inner">
                            <h2><?php _e(\'Profile Sidebar Order\'); ?></h2>
                            <table class="widefat psort-list">
                                <thead>
                                    <tr>
                                        <th><?php _e(\'Name\', \'roots\'); ?></th>
                                        <th><?php _e(\'Order\', \'roots\'); ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php
                                    $count = 0;
                                    foreach($mouldings_options[\'profile_element_order\'] as $key => $item) :
                                        echo \'<tr id="list_items_\' . $key . \'" class="list_item">\';
                                            echo \'<td>\' . $item . \'</td>\';
                                            echo \'<td>\' . $count . \'</td>\';
                                        echo \'</tr>\';
                                        $count++;
                                    endforeach;
                                ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="wp-box-half right">
                        <div class="inner">
                            <?php echo \'<h2>\'.__(\'Information\',\'roots\').\'</h2>\';
                            _e(\'<p>Drag \\\'n drop the order of the elements that appear on the sidebar for both Profiles and Combinations.</p>\',\'roots\'); ?>
                        </div>
                    </div>
                    <div class="clear"></div>
                </div>




            <p class="submit">
                <input type="submit" class="button-primary" value="<?php _e(\'Save Options\',\'roots\'); ?>">
            </p>

            </form>
        </div>

    <?php
    echo ob_get_clean();
}

function mouldings_plugin_admin_styles() {
    wp_enqueue_style(\'mouldings-admin-styles\', MOULDINGS_BASE_URL . \'css/admin-global.css\',\'\',\'\',$media = \'all\');
    wp_enqueue_script(\'jquery-ui-sortable\');
    wp_enqueue_script(\'mouldings-admin-settings-scripts\',  MOULDINGS_BASE_URL . \'js/admin-settings.js\', array(\'jquery\'), \'\', true);
}

function mouldings_add_options_link() {
    $mouldings_options_page = add_options_page(__(\'Mouldings Options\',\'roots\'),__(\'Mouldings\',\'roots\'),\'manage_options\',\'mouldings-options\',\'mouldings_options_page\');
    add_action( \'admin_print_styles-\' . $mouldings_options_page, \'mouldings_plugin_admin_styles\' );
}
add_action(\'admin_menu\',\'mouldings_add_options_link\');

function mouldings_register_settings() {
    register_setting(\'mouldings_settings_group\',\'mouldings_settings\');
}
add_action(\'admin_init\',\'mouldings_register_settings\');

?>
我想这可能与profile_element_order 是否渲染项目?它们是使用此挂钩在激活时正确创建的:

<?php

function mouldings_activate() {
    global $wpdb, $mouldings_options;
    if( false == get_option( \'mouldings_settings\' ) ) {
        $mouldings_options = array(
            \'img_base64_enable\'         => \'1\',
            \'moulding_combination_page\' => \'\',
            \'moulding_collection_page\'  => \'\',
            \'idea_gallery_thumb_height\' => \'200\',
            \'idea_gallery_thumb_width\'  => \'200\',
            \'collection_thumb_height\'   => \'200\',
            \'collection_thumb_width\'    => \'200\',
            \'profile_item_columns\'      => \'4\',
            \'idea_item_columns\'         => \'2\',
            \'collections_item_columns\'  => \'2\',
            \'combinations_item_columns\' => \'4\',
            \'profile_element_order\'     => array(
                \'Option 1\',
                \'Option 2\',
                \'Option 3\',
                \'Option 4\',
                \'Option 5\'
            )
        );
        update_option( \'mouldings_settings\', $mouldings_options );
    }
}
register_activation_hook( MOULDINGS_PLUGIN_FILE, \'mouldings_activate\' );
但在保存时被删除。谢谢

1 个回复
SO网友:Stephen Harris

所以选择profile_element_order forms是一个数组术语,WordPress认为整个数组都是on选项。单击“保存”时,表单数据将发布到WordPress。WordPress将其作为一个数组接收,并通过使用刚接收到的选项数组覆盖旧的选项数组来更新数据库中的选项行。

问题是你的选择profile_element_order 不是接收数组的一部分,因为它没有发布(它只是打印到页面上,但没有输入)。

虽然您试图同时使用AJAX和标准表单进行更新,但您需要同步它们。所以当你的profile_element_order 选项是通过AJAX更新的,它会更新一些隐藏的表单,因此当用户单击“保存”时,表单会发送更新的选项。

但是,为什么要为AJAX而烦恼呢?如果用户要单击保存并发布表单数据,那么这里的AJAX是多余的,只会导致同步问题。

我会使用隐藏的输入来存储订单,当用户重新订购时,这会更新。只有当用户单击“保存”(以及所有其他选项)时,才会保存该订单。

此外,您可能需要阅读设置API。你似乎只使用了它的一部分。。。see this answer (和链接教程)。

结束

相关推荐

wpquery via ajax

我可以重新加载我的页面,让php编写一个新的wpquery 并替换页面上的内容,但我真的不想处理新的页面加载。有没有一种简单的方法wpquery 字符串(例如\"p=7\") 通过ajax? 即使我必须创建一个定制的php页面来进行查询,也没关系。我对wordpress还是新手,所以我想在我开始走“风景”路线之前,确保没有简单的方法可以做到这一点。Thanks.