Adding a Media Button to the WordPress Content Editor
So i'm trying to add a button over the text editor which on pressed lets the user paste in a youtube link which is then covered in shortcode something like this [code]youtube.com[/code]. Following this guide lets me show the button however pressing the button does northing. nothing afterwards pops up.
See screenshots for clarification. And here's my code:
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; // Needed to retrieve our variable in the anonymous function below
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;
}
});
And here's the php inside function.php:
add_action('media_buttons', 'add_my_media_button', 25);
function add_my_media_button() {
echo 'a href=# id=insert-my-media class=buttonAdd my media/a';
}
function include_media_button_js_file() {
wp_enqueue_script('media_button', get_template_directory_uri().'/assets/js/media_button.js');
}
Topic buttons editor html-editor tinymce visual-editor Wordpress
Category Web