使用SAVE_POST上的数组更新wp_Options会导致条目重复

时间:2012-08-24 作者:akamaozu

我不完全确定这是Wordpress的错,但我想我还是会问一下,看看有没有其他人以前遇到过类似的问题。

我正在尝试将数组保存到wp\\u options表行。数据来自自定义元数据库,并在触发save\\u post挂钩时保存。我想这应该可以做到:

update_option("myOptionName", $myArray);
我开始得到4(?)我保存的每个值。例如:

$myArray = array("option1");
update_option("myOptionName", $myArray);
get_option("myOptionName"); 
// array([0]=>"option1", [1]=>"option1", [2]=>"option1", [3]=>"option1");
不用说,这种行为非常令人讨厌。它可以很好地处理字符串,但不能处理数组。我试图通过尝试json\\U编码将其存储为string-y实体来解决这个问题,但即使这样也不起作用。问题很可能是我在这个项目中继承的遗留代码,但我看不到任何指向save\\u post的东西。

以前有人见过这样的情况吗?

编辑:按要求。。。保存功能:

# SAVE BRICK DATA
function brick_update() {

// Verify if this is an auto save routine.
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
    return;
}

// Check permissions
if ( !current_user_can( \'publish_posts\' ) ) { // Check for capabilities
    wp_die( \'Sorry, you do not have the capabilities access to this page. :(\' );
}

if (!wp_verify_nonce($_REQUEST[\'brickupdate\'], \'brickupdate\')) {
    return;
}

$newView = array();
$currentView = get_option(\'ci_guidesbrick_posts\');

if (!is_array($currentView)) {
    $currentView = (array)$currentView;
}

for (var i = 0, i < count($currentView), i++) {
    $newView[i] = $currentView[i];
}

$newView[] = $_REQUEST[\'postID\'];

update_option(\'ci_guidesbrick_posts\', $newView );   

}

4 个回复
SO网友:josh

当我尝试使用wp_editor() 在自定义元数据库中运行。它想多次储蓄。

看看这个。

// SAVE Metabox for admin response
add_action(\'post_updated\', \'display_jwl_wp_etickets_response_meta_box_save\');
function display_jwl_wp_etickets_response_meta_box_save( $post_id ){


        if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
                return;
        if( !isset( $_POST[\'jwl_wp_etickets_editor_box_nonce_check\'] ) || !wp_verify_nonce( $_POST[\'jwl_wp_etickets_editor_box_nonce_check\'], \'jwl_wp_etickets_editor_box_nonce\' ) )
                return;

        global $post;
        $post_type_object = get_post_type_object( $post->post_type );
        if ( !current_user_can( $post_type_object->cap->edit_post, $post->ID ) ) {
                return;
        }

        //$values = get_post_custom( $post_id );

        $editor_id = \'jwl_wp_etickets_response_editor\';
        $meta_key = \'jwl_wp_etickets_response_box_select\';

        $content_post = get_post($post_id);
        $old_content = $content_post->post_content;

        if(isset($_POST[$editor_id]) && !empty($_POST[$editor_id])) {
                if ( !wp_is_post_revision( $post_id ) ){ //IMPORTANT - Can cause infinite loop otherwise : codex - wp_update_post  (see note a few lines down)

                        $new_content = \'<div class="eMember_admin_div"><p class="eMember_adminname_response"><strong>Admin</strong> on <strong>\'.date(\'F j, Y @ g:i a\').\'</strong> said:</p>\'.$_POST[$editor_id].\'</div>\';

                        $update_content = array(
                                \'ID\'           => $post_id,
                                \'post_content\' => $new_content.$old_content
                        );
                        // IMPORTANT!!!!
                        //*****
                        //*****  Apparently the \'post_updated\' action likes to fire on every WP process while saving the content.
                        //*****  Since we are also firing on \'wp_update_post\'; we are getting stuck in a loop.
                        //*****  To get around, unhook the function before sending the revised content with \'wp_update_post\'.
                        //*****  This will prevent clashes between \'post_updated\' and \'wp_update_post" firing at the same time.
                        //*****  DAMN YOU WORDPRESS!!
                        //*****
                        // Unhook this function so it doesn\'t loop infinitely
                        remove_action(\'post_updated\', \'display_jwl_wp_etickets_response_meta_box_save\');

                                // Update the post, which calls save_post again
                                wp_update_post( $update_content );
                                // Let\'s check the \'ticket state\', and if queued... let\'s update it to \'in progress\'
                                $terms_types = wp_get_post_terms( $post->ID, \'jwl_wp_etickets_states\');
                                foreach ($terms_types as $term_type) {
                                        if ($term_type == \'Queued\' || !empty($term_type)) {
                                                wp_set_post_terms( $post_id, __(\'In Progress\',\'wp_etickets_lang\'), \'jwl_wp_etickets_states\' );
                                        }
                                }
                                // Do the same for post meta for cool admin filtering
                                update_post_meta( $post_id, \'jwl_wp_etickets_states_box_select\', __(\'In Progress\',\'wp_etickets_lang\'), __(\'Queued\',\'wp_etickets_lang\') );

                        // Re-hook this function
                        add_action(\'post_updated\', \'display_jwl_wp_etickets_response_meta_box_save\');
                }
        }  

}
请看第43行。看看我是怎么做的remove_action 原始函数。。。运行更新。。。然后add_action 又回来了?

这可能也是你需要做的。

下面是一个快速的“示例”代码:

add_action(\'post_updated\', \'my_metabox_save\');
function my_metabox_save() {
    // Run checks

    // Unhook this function so it doesn\'t loop infinitely
    remove_action(\'post_updated\', \'my_metabox_save\');

    // Run your update stuff

    // Re-hook this function
    add_action(\'post_updated\', \'my_metabox_save\');
}

SO网友:MZAweb

编辑帖子时多次调用save\\u post(自动保存)。这是一个常见的问题,但如果您可以发布函数的代码来查看发生了什么,那就太酷了。

SO网友:janw

就像MZAweb说的那样,这(可能)是自动保存
您应该编辑代码,以便在挂钩触发时检查自动保存:

add_action(\'save_post\', \'my_save_post\', 10, 2);
function my_save_post($post_id, $post) {
    // stop on autosave
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ){
        return;
    }
    // do your magic
}

SO网友:Simon

每次读取整个数组时,我都会在再次创建之前删除该选项:

$newView[] = $_REQUEST[\'postID\'];
delete_option( \'ci_guidesbrick_posts\' );
update_option(\'ci_guidesbrick_posts\', $newView ); 

结束

相关推荐

Validate an option array

我们使用默认的WordPressregister_setting() 函数验证主题选项。此调用,例如:register_setting( \'options-group\', \'option1\', \'intval\' ); 将验证是否$option1 是一个整数。我可以使用什么传递选项数组来验证数组的每个成员?例如:$options = array( \'options1\' => 4, // intval \'options