Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8219749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:17:09+00:00 2026-06-07T13:17:09+00:00

I wrote a very simple plugin to add custom fields to media library but

  • 0

I wrote a very simple plugin to add custom fields to media library but in the database wp_postmeta table shows that there is no meta data for my pdf. I am supposed to get serialized meta data in the meta_value column. Is my plugin incorrect below?
The plugin achieves adding the custom fields but not saving to the database.

enter image description here

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

Plugin Code

  <?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');
    
    ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:17:11+00:00Added an answer on June 7, 2026 at 1:17 pm

    I was missing add_filter(‘attachment_fields_to_save’,’SaveCustomFormFields’,null,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);
    
        ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a very simple mysql line for my very simple search bar that
I have wrote a very simple php upload. But I want to insert image
I'm trying to write a very simple plugin that on anchor mouseover increases the
I wrote a simpe inline edit using jquery. The plugin work very well, but
I wrote a very simple Perl script that contains the following line. my $q_it
I wrote a very simple jsp problem that gets a number from a class
I wrote very simple code to understand how columnstretch and calllater work but I
i wrote a very simple Aspect with Spring AOP. It works, but i have
I wrote a very simple .Net windows application that sits in the system tray
I wrote very simple code to send emails. I wish that end users who

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.