By default, when I click on an item in the media library, it takes me to the Attachment Details page, e.g. .../wp-admin/upload.php?item=65
.
What would I need to edit to make it go straight to the “Edit More Details” page instead? E.g. .../wp-admin/post.php?post=65&action=edit
I assumed it would be as simple as changing where the href is pointing – but there doesn’t seem to be a href there! I’m assuming the link is generated a different way?
From /wp-includes/script-loader.php
:
$scripts->add( 'media-grid', "/wp-includes/js/media-grid$suffix.js", array( 'media-editor' ), false, 1 );
From wp-admin/upload.php
:
wp_enqueue_script( 'media-grid' );
wp_enqueue_script( 'media' );
wp_localize_script( 'media-grid', '_wpMediaGridSettings', array(
'adminUrl' => parse_url( self_admin_url(), PHP_URL_PATH ),
) );
So, we can try to unregister the initial media-grid.js
file and load our own version. This can be done via a plugin:
<?php
/**
* Plugin Name: Change Media Grid link
* Plugin URI: http://wordpress.stackexchange.com/q/168981/17305
*/
add_action( 'admin_enqueue_scripts', 'loadmyscriptinstead_168981' );
function loadmyscriptinstead_168981 (){
wp_deregister_script( 'media-grid' );
wp_register_script( 'media-grid', plugin_dir_url( __FILE__ ) . 'media-grid.js', array( 'media-editor' ), false, 1 );
wp_localize_script( 'media-grid', '_wpMediaGridSettings', array(
'adminUrl' => parse_url( self_admin_url(), PHP_URL_PATH ),
) );
}
The plugin will now load a modified version of the media-grid.js
file found in the plugin folder.
Disclaimer: I have no experience in Backbone, so the following is just a PoC.
This does the job but loads the modal before navigating to the edit page. Someone with Backbone skills may find a cleaner solution.
Change Line 490 from:
// Update browser url when navigating media details
if ( this.model ) {
this.gridRouter.navigate( this.gridRouter.baseUrl( '?item=' + this.model.id ) );
}
to:
// Update browser url when navigating media details
if ( this.model ) {
this.gridRouter.navigate( this.gridRouter.baseUrl( '?item=' + this.model.id ) );
var currentLocation = location.href;
//replace upload.php by post.php
var newRoute = currentLocation.replace('upload.php', 'post.php');
//replace item ?item= by ?post=
newRoute = newRoute.replace('?item=', '?post=");
newRoute += "&action=edit';
window.location = newRoute;
}