I'm creating a plugin that adds a custom button to the default TinyMCE editor in WordPress admin.
The following code builds this simple plugin
mkay-tinymce-extra-button.php
function mkay_enqueue_plugin_script( $plugin_array ){
$plugin_array['subscriptionPlugin'] = plugin_dir_url( __FILE__ ) . 'mkay-tinymce-extra-buttons.js';
return $plugin_array;
}
add_filter( 'mce_external_plugins', 'mkay_enqueue_plugin_script' );
function mkay_register_buttons_editor( $buttons ){
array_push( $buttons, 'subscription' );
return $buttons;
}
add_filter( 'mce_buttons', 'mkay_register_buttons_editor' );
mkay-tinymce-extra-buttons.js
(function(){
tinymce.create('tinymce.plugins.subscriptionPlugin', {
init: function(editor, url){
editor.addButton('subscription', {
title: 'Link per l\'iscrizione',
cmd: 'subscriptionCmd',
icon: false,
text: 'Link iscrizione'
});
editor.addCommand('subscriptionCmd', function(){
var selectedContent = editor.selection.getContent();
var box = '<a href="/iscrizione/?data_corso=[custom field value]" class="subscription-link">'+selectedContent+'</div>';
editor.execCommand('mceInsertContent', 0, box);
});
}
});
tinymce.PluginManager.add('subscriptionPlugin', tinymce.plugins.subscriptionPlugin);
})();
Till here the plugin seems to work well.
Now I need to take the value of a custom field from the post and put it in place of [custom field value] in js file. Then if possible show the custom button only if post is draft or published and the custom field is not empty.
Does someone have any idea? Any help's appreciated.