插件/自定义字段中未调用SAVE_POST,仍在保存覆盖元数据

时间:2014-07-23 作者:SirBangingHeadOnWall

我有一个插件,可以将元盒写入特定页面。它可以很好地编写元数据库,并且一切正常。。。直到我保存。save\\u帖子没有接到电话,我不知道为什么。这是我的_构造中的代码

class Plg_Admin_Meta {

public static function start() {
 return new self();
} 

public function __construct() {
  $this->plugin_url = WP_CONTENT_URL . \'/plugins/\' . basename(dirname(__FILE__));
  $this->plugin_dir = dirname(__FILE__);

  add_action(\'admin_init\', array($this, \'_admin_init\'));  
  add_action(\'save_post\', array($this, \'_save_post\'), 99);
}
元框是由自定义字段创建的,如果编辑自定义字段文本区域,它会很好地保存,但我希望它保存从元框输入的数据。无论出于何种原因,save\\u post都不会启动。我尝试在我的\\u save\\u POST函数的开始处放置一个骰子(var\\u dump($\\u POST)),但它甚至没有启动。

这是我的\\u save\\u post函数

public function _save_post($post_id) {
  //die(dump_var($_POST));
  update_post_meta($post_id, \'content__service_overviews\', "test2");
  // Post meta isn\'t sent for autosaves
  if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
    return;
  }
}

Plg_Admin_Meta::start();
如您所见,我现在只是手动尝试更新字段,但函数甚至没有被调用。

编辑:请求的整个类:

  class Plg_Admin_Meta {

    public static function start() {
     return new self();
    } 

    public function __construct() {
      $this->plugin_url = WP_CONTENT_URL . \'/plugins/\' . basename(dirname(__FILE__));
      $this->plugin_dir = dirname(__FILE__);
      $this->prefix = \'content__\';
      $this->page_fields = array(
        \'site\' => array(
          \'textarea:slideshow\',
          \'text:address\',
          \'text:phone\',
          \'text:fax\',
          \'text:twitter_link\',
          \'text:weather_link\',
          \'text:meta_description\',
          \'text:meta_keywords\',
          \'text:google_analytics_account_number\',
          \'text:google_site_verification_code\'
        ),
        \'home\' => array(
          \'textarea:about_enernex\',
          \'textarea:introducing_utility_stats\',
          \'textarea:recent_content\',
          \'textarea:workshop_button\'
        ),
        \'about\' => array(
          \'textarea:what_we_do_photo\',
          \'textarea:what_we_do\',
          \'textarea:our_mission_photo\',
          \'textarea:our_mission\',
          \'textarea:get_to_know_us_photo\',
          \'textarea:get_to_know_us_principals\',
          \'textarea:get_to_know_us_staff\'
        ),
        \'careers\' => array(
          \'textarea:why_enernex_photo\',
          \'textarea:why_enernex\',
          \'textarea:support_our_mission_photo\',
          \'textarea:support_our_mission\',
          \'textarea:the_innovation_valley_photo\',
          \'textarea:the_innovation_valley_our_location\'
        ),
        \'contact\' => array(
          \'textarea:photo\',
          \'textarea:contact_information\',
          \'textarea:map_image\',
          \'textarea:directions\'
        ),
        \'employees\' => array(
          \'text:title\',
          \'textarea:text\',
          \'textarea:photo\'
        ),
        \'support\' => array(
          \'textarea:announcing_utility_stats\',
          \'textarea:faq\',
          \'textarea:utility_ownership_key\',
          \'textarea:support\'
        ),
        \'newsroom\' => array(
          \'textarea:service_overviews\',
          \'textarea:media_contacts\',
          \'textarea:industry_resources_and_related_links\'
        ),
        \'services\' => array(
          \'textarea:main_text\',
          \'textarea:power_systems_consulting_photo\',
          \'textarea:smart_grid_engineering_photo\',
          \'textarea:smart_grid_labs_photo\',
          \'textarea:workshops_photo\'
        ),
        \'iec-workshop\' => array(
          \'textarea:first_photo\',
          \'textarea:course_overview\',
          \'textarea:topics_covered\',
          \'textarea:our_instructors\',
          \'textarea:workshop_dates\',
          \'textarea:who_should_attend\',
          \'textarea:key_benefits\',
          \'textarea:schedule_location_and_cost\',
          \'textarea:second_photo\',
          \'textarea:location\'
        ),
        \'cyber-security\' => array(
          \'textarea:meet_the_team\',
          \'text:media_kit_download_link\'
        )
      );

      $this->post_fields = array(
        \'news\' => array(
          \'textarea:overview\',
          \'textarea:story\'
        ),
        \'press\' => array(
          \'textarea:overview\',
          \'textarea:story\'
        ),
        \'whitepapers\' => array(
          \'textarea:overview\',
          \'textarea:uploads\'
        ),
        \'areas\' => array(
          \'textarea:overview\',
          \'textarea:uploads\',
          \'textarea:projects\'
        ),
        \'links\' => array(
          \'text:URL\'
        ),
        \'jobs\' => array(
          \'textarea:basic_function\',
          \'textarea:education_and_experience\',
          \'textarea:skills\',
          \'textarea:duties_and_responsibilities\',
          \'textarea:contact_information\',
          \'textarea:notes\'
        ),
        \'employees\' => array(
          \'text:position\',
          \'text:department\',
          \'text:year_joined\',
          \'text:power_industry_years\',
          \'text:education\',
          \'text:expertise\',
          \'text:phone\',
          \'text:email_address\',
          \'textarea:biography\',
          \'textarea:headshot\'
        ),
        \'blog\' => array(
          \'textarea:author\',
          \'textarea:content\'
        ),
        \'announcements\' => array(
          \'text:link_to_announcement\',
          \'textarea:content\'
        )
      );



      add_action(\'admin_init\', array($this, \'_admin_init\'));  
      add_action(\'save_post\', array($this, \'_save_post\'), 99, 2);   



    }


    public function _admin_init() {

      $post_id = isset($_REQUEST[\'post\']) ? $_REQUEST[\'post\'] : false;
      if ($post_id) {

        $content = get_post($post_id);
        $slug = $content->post_name;
        $post_type = $content->post_type;

        if (isset($post_type)) {

          switch($post_type) {

            case \'page\':
              $this->_create_metaboxes(\'page\', $this->page_fields[$slug]);
            break;

            case \'post\':
              $category = get_the_category($post_id);
              $slug = $category[0]->slug;
              $this->_create_metaboxes(\'post\', $this->post_fields[$slug]);
            break;

          }

        }

      }

    }

    public function _create_metabox_content($post, $metabox) {

      $post_id = $post->ID;   
      $meta_key = $metabox[\'args\'][\'meta_key\'];
      $meta_value = get_post_meta($post_id, $meta_key, true);

      switch ($metabox[\'args\'][\'type\']) {

        case \'text\':

          ?>

          <input type="text" name="<?php echo $meta_key; ?>" value="<?php echo $meta_value; ?>" style="font-family:\'Georgia\', \'Times New Roman\', \'Bistream Charter\', \'Times\', \'serif\';font-size: 13px; line-height: 19px; color: #333; width: 100%;" />

          <?php

        break;

        case \'textarea\':

          wp_editor( 
            $meta_value,
            $meta_key,
            array(
              \'textarea_name\' => $meta_key,
              \'textarea_rows\' => 4,
              \'tinymce\' => array(
                \'content_css\' => $this->plugin_url . \'/tinymce.css\'
              )
            )        
          );     

        break;

      }

    }

    public function _create_metaboxes($post_type, $fields_array) {

      if (!empty($fields_array)) {

          wp_enqueue_style(\'admin\', $this->plugin_url . \'/admin.css\');

            foreach ($fields_array as $field) {

              $tokens = explode(\':\', $field);
              $type = $tokens[0];
              $meta_key = $this->prefix . $tokens[1];
              $id = \'metabox_\' . $tokens[1];
              $title = ucwords(preg_replace(\'/\\_/\', \' \', $tokens[1]));            

              add_meta_box(
                $id,
                $title,
                array($this, \'_create_metabox_content\'),
                $post_type,
                \'normal\',
                \'high\',
                array(
                  \'meta_key\' => $meta_key,
                  \'type\' => $type
                )                  
              );

            }

      }

    }  

    public function _save_post($post_id) {

    //die(var_dump($_POST));
    // Post meta isn\'t sent for autosaves
    if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
      return;
    }

    update_post_meta($post_id, \'content__service_overviews\', "test2");


      if (isset($_REQUEST[\'post_ID\'])) {


          foreach ($_REQUEST as $meta_key => $meta_value) {
            if (strstr($meta_key, $this->prefix)) {

            //update_post_meta(intval($_REQUEST[\'post_ID\']), meta_key, meta_value);

            }

          }


      }

    }


  }


  Plg_Admin_Meta::start();

1 个回复
最合适的回答,由SO网友:SirBangingHeadOnWall 整理而成

所以我发现问题出在数据库里。如果我删除了页面,然后重新创建它,那么它就会正常工作。我无法缩小数据的确切错误,但确实有问题。

那一页确实有很多修改,这不重要吗?

结束

相关推荐

如何隐藏,而不是去掉属性的Metabox?

我需要hide, 并且不要删除后端中的属性元框。以下代码正在删除元数据库:function hide_meta_box_attributes() { remove_meta_box(\'pageparentdiv\', \'post\', \'normal\'); } add_filter(\'add_meta_boxes\', \'hide_meta_boxes_attributes\'); 问题是,我运行的是分层帖子,这意味着我需要有可能拥有此元数据库启用的帖子和子帖子。当