Alter image meta fields in the Media Library

To improve user experience, I want to make changes to the image meta fields in the Media Library:

Image Meta Fields

Read More

As demonstrated above, I want to change the Caption field to Source and remove the Alt Text and Description field.

How can I achieve this?

Related posts

2 comments

  1. I don’t know if this answer will be of much use given the age of this question now, but perhaps it will provide an answer sufficient that you can at least consider the question answered.

    I would point out this approach involves creating copies of WordPress functions, modifying them and repointing WP hooks (unhooking and rehooking), so it might be susceptible to breaking if core functionality around these functions and hooks changes such that the copied code is no longer inline with what’s in core.

    The complete code exceeds the answer character limit, so here’s the class without the two copied/modified functions from wp core (see pastebin at the end for complete code).

    class WPSE_Question_134092 {
        
        public function __construct() {
            add_action( 'add_meta_boxes_attachment', [$this, 'on_attachment_metaboxes'] );
            add_action( 'wp_enqueue_media',          [$this, 'on_media_enqueue'] );
        }
        public function on_attachment_metaboxes() {
            remove_action( 'edit_form_after_title', 'edit_form_image_editor' );
            add_action( 'edit_form_after_title', [$this,'edit_form_image_editor'] );
        }
        public function on_media_enqueue() {
            remove_action( 'admin_footer',                            'wp_print_media_templates' );
            remove_action( 'wp_footer',                               'wp_print_media_templates' );
            remove_action( 'customize_controls_print_footer_scripts', 'wp_print_media_templates' );
            
            add_action( 'admin_footer',                            [$this, 'print_media_templates'] );
            add_action( 'wp_footer',                               [$this, 'print_media_templates'] );
            add_action( 'customize_controls_print_footer_scripts', [$this, 'print_media_templates'] );
        }
    }
    

    Core WP functions:

    https://developer.wordpress.org/reference/functions/edit_form_image_editor/
    https://developer.wordpress.org/reference/functions/wp_print_media_templates/

    Pastebin with modified functions:

    https://pastebin.com/p4KH3g6v

    Image fields should look something like.

    enter image description here

    And all i’ve done with these two functions is copy them straight form core, scan down the files and rename or comment out(you can straight up delete those sections of code if desired) the appropriate fields to match what you asked for. If i missed a detail or accidently commented something out that shouldn’t have been, it should be trivial to tweak.

  2. I found this great tutorial for adding/filtering fields. However, do you think it is ideal remove core functionality? I suggest hiding the fields with css that you do not want displayed, and creating a field for Source rather than modifying it. The reason for this is so that other devs or plugins can utilize the core functionality with your plugin or theme without a lot of workaround.

Comments are closed.