Created
February 19, 2025 05:30
-
-
Save brandonjp/5dfe41ba8c24f7e4dce610febad3e13f to your computer and use it in GitHub Desktop.
Media Library Double-Click Selection [SnipSnip.pro] - https://snipsnip.pro/s/858 - Enhances the WordPress Media Library modal by enabling double-click to select and confirm media items. Double-clicking an image automatically selects it and triggers the select/insert button, streamlining the media selection workflow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Title: Media Library Double-Click Selection [SnipSnip.pro] | |
* Description: Enhances the WordPress Media Library modal by enabling double-click to select and confirm media items. Double-clicking an image automatically selects it and triggers the select/insert button, streamlining the media selection workflow. | |
* Version: 2.2.0 | |
* Author: brandonjp.com | |
* Last Updated: 2025-02-18 | |
* Blog URL: https://snipsnip.pro/s/858 | |
* Requirements: WordPress 5.0 or later | |
* License: GPL v2 or later | |
*/ | |
if (!class_exists('WP_Media_Library_Double_Click_Handler')): | |
class WP_Media_Library_Double_Click_Handler { | |
const VERSION = '2.2.0'; | |
public function __construct() { | |
add_action('admin_enqueue_scripts', array($this, 'add_scripts')); | |
} | |
public function add_scripts() { | |
if (!is_admin()) { | |
return; | |
} | |
$js = <<<'JAVASCRIPT' | |
window.addEventListener("load", function() { | |
(function createMediaLibraryDoubleClick() { | |
"use strict"; | |
function handleDoubleClick(event) { | |
event.preventDefault(); | |
event.stopPropagation(); | |
const attachment = jQuery(event.currentTarget); | |
// Force selection if not already selected | |
if (!attachment.hasClass('selected')) { | |
attachment.click(); | |
} | |
// Small delay to ensure selection is processed | |
setTimeout(() => { | |
const selectButton = jQuery('.media-modal .media-toolbar .media-button-select, .media-modal .media-toolbar .button-primary'); | |
if (selectButton.length && !selectButton.prop('disabled')) { | |
selectButton.click(); | |
} | |
}, 50); | |
} | |
function init() { | |
// Remove any existing handlers | |
jQuery(document).off('dblclick', '.media-modal .attachments-browser .attachments .attachment'); | |
// Add double-click handler | |
jQuery(document).on('dblclick', '.media-modal .attachments-browser .attachments .attachment', handleDoubleClick); | |
// Add handler for classic editor (if present) | |
if (typeof wp.media.frame !== 'undefined') { | |
wp.media.frame.on('open', function() { | |
setTimeout(init, 100); | |
}); | |
} | |
} | |
// Initialize when document is ready | |
init(); | |
// Initialize for Gutenberg | |
if (window.wp && wp.data && wp.data.subscribe) { | |
wp.data.subscribe(function() { | |
const coreEditor = wp.data.select('core/editor'); | |
if (coreEditor && coreEditor.isCleanNewPost) { | |
init(); | |
} | |
}); | |
} | |
})(); | |
}); | |
JAVASCRIPT; | |
wp_add_inline_script('jquery', $js); | |
} | |
} | |
endif; | |
if (class_exists('WP_Media_Library_Double_Click_Handler')): | |
new WP_Media_Library_Double_Click_Handler(); | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment