I think your real problem here though is timing, in that you should only do the remove_action
and add_action
in the wp_enqueue_media
action fired at the end of the wp_enqueue_media()
function, after it has added the action you’re replacing:
add_action( 'wp_enqueue_media', function () {
if ( ! remove_action( 'admin_footer', 'wp_print_media_templates' ) ) {
error_log("remove_action fail");
}
add_action( 'admin_footer', 'my_print_media_templates' );
} );
(My guess would be that the remove_action()
you’re doing is actually failing, and that wp_print_media_templates()
is still being called, but after your my_print_media_templates()
, so that the DOM has two copies of the media templates, but yours wins because it comes first. And though I don’t understand why you would have to do your own require_once
of “media-template.php”, it’s a pretty big flashing warning light!)
Also, though this is a personal preference, I’d prefer to manipulate the existing templates (after calling the original wp_print_media_templates()
) rather than just copy and paste the whole shabang and edit them. Eg:
function my_print_media_templates() {
$replaces = array(
'/<option value="center"https://wordpress.stackexchange.com/" => '<option value="leftsuper">' . esc_attr__('LeftSuper') . '</option>$0',
'/<option value="none"https://wordpress.stackexchange.com/" => '<option value="rightsuper">' . esc_attr__('RightSuper') . '</option>$0',
'/<button class="button" value="center">/' => '<button class="button" value="leftsuper">' . esc_attr__('LeftSuper') . '</button>$0',
'/<button class="button active" value="none">/' => '<button class="button" value="rightsuper">' . esc_attr__('RightSuper') . '</button>$0',
);
ob_start();
wp_print_media_templates();
echo preg_replace( array_keys( $replaces ), array_values( $replaces ), ob_get_clean() );
}
I think this is a bit more maintainable than the copy & paste approach, which is almost guaranteed to go out-of-date on each new release of WP.