【问题标题】:Editing serialized data WordPress dashboard编辑序列化数据 WordPress 仪表板
【发布时间】:2017-04-12 15:13:46
【问题描述】:

我刚刚继承了一个自定义插件,它接受 Formstack 提交并从中创建 WordPress 帖子。帖子创建得很好,但是表单内容作为序列化数据存储在 post_content 中。

我的任务是使这些帖子能够在 WP 仪表板中进行编辑。目前,当您单击帖子标题时,您会看到一个仅显示数据的页面;无法编辑数据。

在 functions.php 文件中的“supports”中启用编辑器控件会为我提供编辑器,其中包含刚刚转储到编辑器中的序列化数据。

我从来不需要为 WP 中的特定帖子类型设置自定义编辑页面。有没有人可以指导我这样一个解释这一点的网站?我在绕圈跑。

【问题讨论】:

    标签: php wordpress dashboard


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      您可以在内容显示在管理员编辑屏幕之前对其进行过滤。

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

      要运行此脚本,您可以简单地创建一个临时的新页面,然后专门为该页面创建一个模板文件,并将上述代码放入该文件(然后访问该页面)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-19
        • 1970-01-01
        • 2014-05-09
        • 1970-01-01
        • 2011-01-20
        • 2023-03-31
        • 2011-06-22
        • 1970-01-01
        相关资源
        最近更新 更多