-
-
Save haciyevmayis/62e91b9a84e35d5549e08687b0731b54 to your computer and use it in GitHub Desktop.
Adding a Media Button to the WordPress Editor.
This file contains hidden or 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
jQuery(function($) { | |
$(document).ready(function(){ | |
$('#insert-my-media').click(open_media_window); | |
}); | |
function open_media_window() { | |
if (this.window === undefined) { | |
this.window = wp.media({ | |
title: 'Insert a media', | |
library: {type: 'image'}, | |
multiple: false, | |
button: {text: 'Insert'} | |
}); | |
var self = this; | |
this.window.on('select', function() { | |
var first = self.window.state().get('selection').first().toJSON(); | |
wp.media.editor.insert('[myshortcode id="' + first.id + '"]'); | |
}); | |
} | |
this.window.open(); | |
return false; | |
} | |
}); |
This file contains hidden or 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 | |
// Add Btn after 'Media' | |
add_action( 'media_buttons', 'add_my_media_button', 15); | |
function add_my_media_button() { | |
echo '<a href="#" id="insert-my-media" class="button">Add my media</a>'; | |
} | |
// Add js to admin | |
add_action( 'wp_enqueue_media', 'include_media_button_js_file' ); | |
function include_media_button_js_file() { | |
wp_enqueue_script( 'media_button', plugin_dir_url( __FILE__ ) . 'assets/admin.js', array('jquery'), '1.0', true ); | |
} | |
// Inserts new tab to wp.media | |
add_filter( 'media_upload_tabs', 'my_media_menu' ); | |
function my_media_menu( $tabs ) { | |
$newtab = array( 'my_custom_tab' => __('My Tab') ); | |
return array_merge($tabs, $newtab); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment