2016-11-28 22 views
0

我剛剛繼承了一個自定義插件,它需要Formstack提交併從它們創建WordPress帖子。帖子創建正常,但表單內容作爲序列化數據存儲在post_content中。編輯序列化數據WordPress儀表板

我的任務是使這些帖子能夠在WP儀表板中編輯。目前,當您單擊帖子標題時,會看到一個只顯示數據的頁面;沒有能力編輯數據。

在functions.php文件的「支持」中啓用編輯器控件,使編輯器可以看到編輯器中剛剛轉儲的序列化數據。

我從來不必爲WP中的特定帖子類型設置自定義編輯頁面。有沒有人可以指導我解釋這個問題?我在圈子裏跑。

回答

0

它在管理編輯畫面呈現之前,您可以過濾內容。

function my_filter_function_name($content, $post_id) { 
    if(get_post_type($post_id) == 'the_post_type_in_question'){ 
     $serialized_content = $content; 
     $content_array = unserialize($serialized_content); 
     // do something with this array to put it in the format you want 
     // ..... 
     $content = $new_formatted_content; 
    } 
    return $content; 
} 
add_filter('content_edit_pre', 'my_filter_function_name', 10, 2); 

但是,這似乎不會對你有多大用處。

在你的情況下,我建議你花時間寫一個腳本來轉換所有這些帖子,以便所有東西都被存儲爲post meta。 (首先創建自定義字段)。

如果您的主題不是基於任何框架構建的,那麼我認爲創建自定義字段的最快方法是使用Advanced Custom Fields plugin

然後,一旦您知道meta_keys,您可以編寫該腳本。例如。

$posts = get_posts('post_type'=>'the_post_type','posts_per_page'=> -1); 

foreach($posts as $post){ 

    $content_array = unserialize($post->post_content); 

    // how you do the next bit will depend on whether or not this is an associative array. I'm going to assume it is (because it's a little easier :)) 

    foreach($content_array as $meta_key=>$meta_value){ 
     update_post_meta($post->ID, $meta_key, $meta_value); 
    } 

    // just put what you actually want as the post content back into the post content: 
    wp_update_post(array('ID'=>$post->ID,'post_content'=>$content_array['post_content'])); // assuming the key of the element you want to be the post content is 'post_content' 

} 

要運行此腳本,你可以簡單地創建一個暫新的頁面,然後創建一個特定的模板文件,該頁面,並把上面的代碼到該文件(然後訪問該頁面)。