Modifying the media-template.php file, the right way?

You’re in a pretty good pickle here.

There are a couple things you can do as far as I can tell. Both of them are a little tricky.

Recommended

The one I would recommend would be to make use of the do_action('print_media_templates') at the bottom of that file. It’s not perfect, but you could add in an additional field here to take care of the responsive aspect you want to add.

Possible, but not recommended

Another option is to actually modify the actions that are being set in /wp-includes/media.php lines 2022-2024. Check this out:

function custom_print_media_templates() {
  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', 'custom_print_media_templates' );
  add_action( 'wp_footer', 'custom_print_media_templates' );
  add_action( 'customize_controls_print_footer_scripts', 'custom_print_media_templates' );
}
add_action( 'wp_enqueue_media', 'custom_print_media_templates' );

You would then have to go create a function called custom_print_media_templates() that mirrors the one in wp-includes/media-template.php. The problem here is that any changes to WordPress core for this file would need to be replicated in your file. That’s why I don’t recommend this path.

Leave a Comment