2012-07-13 158 views
0

我写了一个非常简单的插件来将自定义字段添加到媒体库,但是在数据库中wp_postmeta表显示没有我的pdf的元数据。我应该在meta_value列中获取序列化元数据。我的插件不正确? 插件实现添加自定义字段但不保存到数据库。WordPress将自定义字段保存到数据库

enter image description here

meta_id post_id  meta_value 
552 356   a:0:{} 
551 356 

插件代码

<?php 
    /* 
    Plugin Name: Media Library Fields 
    Plugin URI: http://mhmintranet 
    Description: The plugin adds additional fields to the media library 
    Version: 1.0 
    Author: me 
    Author URI: Metropolitan State Hospital http://mhmintranet 
    License: GPL2 
    */ 
    function GetCustomFormFields($form_fields, $post) 
    { 
     $form_fields['RevisionDate'] = array(

       'label' => 'Revision Date', 
       'input' => 'text', 
       'value' => get_post_meta($posdt->ID, '_RevisionDate',true), 
       'helps' => 'This is the date the document was revised last' 
       ); 

     $form_fields['ADTitle'] = array(

       'label' => 'AD Title', 
       'input' => 'text', 
       'value' => get_post_meta($posdt->ID, '_ADTitle',true), 
       'helps' => 'Administrative Directive Title' 
       ); 
     $form_fields['AdNumber'] = array(

       'label' => 'AD Number', 
       'input' => 'text', 
       'value' => get_post_meta($posdt->ID, '_AdNumber',true), 
       'helps' => 'Administrative Directive Title' 
       ); 

     return $form_fields; 
    } 
    add_filter("attachment_fields_to_edit", "GetCustomFormFields", null, 2); 

    function SaveCustomFormFields($post,$attachment) 
    { 
     if(isset($attachment['RevisionDate'])) 
     { 
      update_post_meta($post['ID'], '_RevisionDate', $attachment['RevisionDate']); 
     } 
    if(isset($attachment['ADTitle'])) 
     { 
      update_post_meta($post['ID'], '_ADTitle', $attachment['ADTitle']); 
     } 
    if(isset($attachment['AdNumber'])) 
     { 
      update_post_meta($post['ID'], '_AdNumber', $attachment['AdNumber']); 
     } 
     return $post; 
    } 
    add_filter('attachment_fields_to_save','SaveCustomFormFields'); 

    ?> 

回答

0

我是缺少的add_filter( 'attachment_fields_to_save', 'SaveCustomFormFields',零,2);

<?php 
    /* 
    Plugin Name: Media Library Fields 
    Plugin URI: http://mhmintranet 
    Description: The plugin adds additional fields to the media library 
    Version: 1.0 
    Author: Jose Velez 
    Author URI: Metropolitan State Hospital http://mhmintranet 
    License: GPL2 
    */ 

    //Function used 
    /*get_post_meta :http://codex.wordpress.org/Function_Reference/get_post_meta 

    attachment_fields_to_edit and attachment_fields_to_save is a hook located wp-admin/includes/media.php 
    To learn more: 
    http://net.tutsplus.com/tutorials/wordpress/creating-custom-fields-for-attachments-in-wordpress/ 
    http://codex.wordpress.org/Plugin_API/Filter_Reference/attachment_fields_to_save 

    Weird Wordpress convention : Fields prefixed with an underscore 
    (_RevisionDate) will not be listed in the drop down of available custom fields on the post/page screen; 
    I only need the custom fields in the media library page 
    */ 
    function GetCustomFormFields($form_fields, $post) 
    { 
     $form_fields['RevisionDate'] = array(

       'label' => 'Revision Date', 
       'input' => 'text', 
       'value' => get_post_meta($posdt->ID, '_RevisionDate',true), 
       'helps' => 'This is the date the document was revised last' 
       ); 

     $form_fields['ADTitle'] = array(

       'label' => 'AD Title', 
       'input' => 'text', 
       'value' => get_post_meta($posdt->ID, '_ADTitle',true), 
       'helps' => 'Administrative Directive Title' 
       ); 
     $form_fields['AdNumber'] = array(

       'label' => 'AD Number', 
       'input' => 'text', 
       'value' => get_post_meta($posdt->ID, '_AdNumber',true), 
       'helps' => 'Administrative Directive Title' 
       ); 

     return $form_fields; 
    } 
    add_filter("attachment_fields_to_edit", "GetCustomFormFields", null, 2); 

    function SaveCustomFormFields($post,$attachment) 
    { 
     if(isset($attachment['RevisionDate'])) 
     { 
      update_post_meta($post['ID'], '_RevisionDate', $attachment['RevisionDate']); 
     } 
    if(isset($attachment['ADTitle'])) 
     { 
      update_post_meta($post['ID'], '_ADTitle', $attachment['ADTitle']); 
     } 
    if(isset($attachment['AdNumber'])) 
     { 
      update_post_meta($post['ID'], '_AdNumber', $attachment['AdNumber']); 
     } 
     return $post; 
    } 
    add_filter('attachment_fields_to_save','SaveCustomFormFields',null,2); 

    ?> 
相关问题