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 8646657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:52:12+00:00 2026-06-12T12:52:12+00:00

I’m trying to add a custom field to the media uploader in WordPress. I

  • 0

I’m trying to add a custom field to the media uploader in WordPress. I ahve it working, but I’d like to make the custom field key a hidden one.

If you are familiar with the way WordPress handles custom fields, you’ll know that setting the key to “_something” will hide that key from the drop down lists visible to the user.

/**
 * Add Video URL fields to media uploader
 *
 * http://www.billerickson.net/wordpress-add-custom-fields-media-gallery/
 *
 * @param $form_fields array, fields to include in attachment form
 * @param $post object, attachment record in database
 * @return $form_fields, modified form fields
 */

    function capgun2012_attachment_fields( $form_fields, $post ) {

        $form_fields['capgun2012_video_url'] = array(
            'label' => 'Vimeo URL',
            'input' => 'text',
            'value' => get_post_meta( $post->ID, 'capgun2012_video_url', true ),
            'helps' => 'If provided, photo will be displayed as a video',
        );

        return $form_fields;
    }

    add_filter( 'attachment_fields_to_edit', 'capgun2012_attachment_fields', 10, 2 );

/**
 * Save values of Photographer Name and URL in media uploader
 *
 * @param $post array, the post data for database
 * @param $attachment array, attachment fields from $_POST form
 * @return $post array, modified post data
 */

    function capgun2012_attachment_fields_save( $post, $attachment ) {

        if( isset( $attachment['capgun2012_video_url'] ) ) {
            update_post_meta( $post['ID'], 'capgun2012_video_url', $attachment['capgun2012_video_url'] );
        }

        return $post;
    }

    add_filter( 'attachment_fields_to_save', 'capgun2012_attachment_fields_save', 10, 2 );  

If I simply replace all the occurrences of “capgun2012_video_url” with “_capgun2012_video_url” then it doesn’t work. I’m starting to think that the media uploader doesn’t play well with hidden custom fields.

Please see attached screenshot of what I don’t want to happen (the custom key showing in the custom fields drop down).

enter image description here

Thanks for the help.

  • 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-12T12:52:13+00:00Added an answer on June 12, 2026 at 12:52 pm

    This was answered here:
    http://devilsworkshop.org/adding-custom-fields-to-wordpress-media-gallery-upload/

    Setup field:

       /* For adding custom field to gallery popup */
    function rt_image_attachment_fields_to_edit($form_fields, $post) {
        // $form_fields is a an array of fields to include in the attachment form
        // $post is nothing but attachment record in the database
        //     $post->post_type == 'attachment'
        // attachments are considered as posts in WordPress. So value of post_type in wp_posts table will be attachment
        // now add our custom field to the $form_fields array
        // input type="text" name/id="attachments[$attachment->ID][custom1]"
        $form_fields["rt-image-link"] = array(
            "label" => __("Custom Link"),
            "input" => "text", // this is default if "input" is omitted
            "value" => get_post_meta($post->ID, "_rt-image-link", true),
                    "helps" => __("To be used with special slider added via [rt_carousel] shortcode."),
        );
       return $form_fields;
    }
    // now attach our function to the hook
    add_filter("attachment_fields_to_edit", "rt_image_attachment_fields_to_edit", null, 2);
    

    And this to save it:

    function rt_image_attachment_fields_to_save($post, $attachment) {
        // $attachment part of the form $_POST ($_POST[attachments][postID])
            // $post['post_type'] == 'attachment'
        if( isset($attachment['rt-image-link']) ){
            // update_post_meta(postID, meta_key, meta_value);
            update_post_meta($post['ID'], '_rt-image-link', $attachment['rt-image-link']);
        }
        return $post;
    }
    // now attach our function to the hook.
    add_filter("attachment_fields_to_save", "rt_image_attachment_fields_to_save", null , 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.