Skip to content

Instantly share code, notes, and snippets.

@mcguffin
Created December 27, 2014 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcguffin/07676b3a25b6258a9e33 to your computer and use it in GitHub Desktop.
Save mcguffin/07676b3a25b6258a9e33 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Universal Media Library
Description: Make the pretty post/page media library accessibe from anywhere
Author: Jörn Lund
Author URI: http://github.org/mcguffin
Version: 0.0.1
*/
function universal_media_library_js() {
?><script type="text/javascript">
function open_media_frame() {
if ( ! wp.media.frames.universal_frame ) {
wp.media.frames.universal_frame = wp.media({
title: 'Select image',
multiple: false,
button: {
text: 'Use selected image'
}
});
}
wp.media.frames.universal_frame.open();
}
</script><?php
}
// add some script
add_action( 'admin_footer', 'universal_media_library_js' );
function add_admin_bar_media_library_link( $wp_admin_bar ) {
$add_menu_args = array(
'id' => 'media_library',
'title' => __('Media Library'),
'href' => '#',
'meta' => array(
'onclick' => 'open_media_frame();return false;'
),
);
$wp_admin_bar->add_menu( $add_menu_args );
}
// put a link in the admin bar
add_action( 'admin_bar_menu', 'add_admin_bar_media_library_link' , 200);
// enqueue media anywhere in wp-admin
add_action( 'admin_print_scripts' , 'wp_enqueue_media' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment