0
votes

I just inherited a custom plugin that takes Formstack submissions and creates WordPress posts from them. The posts are created fine, however the form contents are stored as serialized data in post_content.

I am tasked with enabling these posts to be edited within the WP Dashboard. Currently when you click on a post title, you are presented with a page that just shows the data; no capability to edit the data.

Enabling the editor controls within "supports" in the functions.php file gives me the editor with the serialized data just dumped in the editor.

I have never had to setup a custom edit page for a specific post type in WP. Is there someone out there who can direct me so a site that explains this? I'm running in circles.

2

2 Answers

0
votes
0
votes

You can filter the content before it is presented in the admin editing screen.

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 );

But, that doesn't seem like it's going to be of much use to you.

In your situation, I suggest you take the time to write a script to convert all those posts so that everything is stored as post meta. (create the custom fields, first).

If your theme isn't built on any framework, then I think the quickest way to create a custom field is to use the Advanced Custom Fields plugin.

Then, once you know the meta_keys, you can write that script. E.g.

$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'

}

To run this script, you could simply create a temporary new page and then create a template file specifically for that page and put the above code into that file (then visit the page).