Hi all – I’m attempting to write a simple plugin that adds two buttons to the wordpress classic editor toolbar – one for aligning text left, ignoring the indents of the theme, and has a simple structure. The second, merely addes a stylized <pre> function so we can post irregular lines of poetry. I can get the buttons to show up in the toolbar, but am receiving a “plugin failed to initialize” error and it just doesn’t work. I feel like I’m missing something basic. Can anyone take a look at my code and see if you spot anything amiss?
P.S. We’re not using CSS or shortcodes because my staff can’t/won’t use any code, even easy code. I’m trying to rule out formatting mistakes by making a one-click option for them. Thanks!!
JAVASCRIPT:
(function() {
tinymce.PluginManager.add(‘poetry_formatter’, function(editor, url) {
// Add the custom button for left-aligned text
editor.addButton(‘poetry_formatter_left’, {
text: ‘Left Align Poetry’,
icon: ‘https://fivesouthstaff.com/wp-content/plugins/poetry_formatter/images/left-aligned-button.png’,
onclick: function() {
// Apply formatting for left-aligned poetry
editor.formatter.apply(‘poetry_formatter_left_format’);
}
});
// Add the custom button for preformatted text
editor.addButton(‘poetry_formatter_preformatted’, {
text: ‘Preformatted Poetry’,
icon: ‘https://fivesouthstaff.com/wp-content/plugins/poetry_formatter/images/preformatted-button.png’,
onclick: function() {
// Apply formatting for preformatted poetry
editor.formatter.apply(‘poetry_formatter_preformatted_format’);
}
});
// Define the formatting options for left-aligned poetry
editor.formatter.register(‘poetry_formatter_left_format’, {
block: ‘p’,
styles: {
‘text-align’: ‘left’,
‘font-size’: ’18px’,
‘font-family’: ‘EB Garamond, serif’,
‘line-height’: ‘1.5’,
‘margin’: ‘0’,
‘padding’: ‘0’,
‘list-style-type’: ‘none’
}
});
// Define the formatting options for preformatted poetry
editor.formatter.register(‘poetry_formatter_preformatted_format’, {
block: ‘pre’,
styles: {
‘font-size’: ’18px’,
‘font-family’: ‘EB Garamond, serif’,
‘line-height’: ‘1.5’,
‘white-space’: ‘pre-wrap’
}
});
});
})();
\_\_\_\_\_\_\_\_\_\_\_
PHP
<?php
/*
Plugin Name: poetry_formatter
Description: Add custom formatting buttons for poetry.
Version: 1.0
Author: Kristen S.
*/
// Enqueue the JavaScript file for the plugin
function poetry_formatter_enqueue_scripts() {
wp_enqueue_script(‘poetry-formatter-script’, plugins_url(‘poetry_formatter.js’, __FILE__), array(‘jquery’), ‘1.0’, true);
}
add_action(‘admin_enqueue_scripts’, ‘poetry_formatter_enqueue_scripts’);
// Add custom buttons to the WordPress editor toolbar
function poetry_formatter_add_buttons() {
add_filter(‘mce_external_plugins’, ‘poetry_formatter_register_buttons’);
add_filter(‘mce_buttons’, ‘poetry_formatter_add_button’);
}
add_action(‘init’, ‘poetry_formatter_add_buttons’);
// Register the JavaScript file for the buttons
function poetry_formatter_register_buttons($plugin_array) {
$plugin_array[‘poetry_formatter’] = plugins_url(‘poetry_formatter.js’, __FILE__);
return $plugin_array;
}
// Add the custom buttons to the toolbar
function poetry_formatter_add_button($buttons) {
array_push($buttons, ‘poetry_formatter_left’, ‘poetry_formatter_preformatted’);
return $buttons;
}
​
[ad_2]