-
-
Save tommcfarlin/2dc7ed7a7624efdabfb8 to your computer and use it in GitHub Desktop.
[WordPress] Adding a custom button to the TinyMCE editor
This file contains 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 | |
public function mce_button() { | |
add_filter( | |
'mce_external_plugins', | |
array( $this, 'add_button' ) | |
); | |
} | |
public function add_button( $plugin_array ) { | |
$plugin_array['yhd'] = plugin_dir_url( dirname( __FILE__ ) ) . 'admin/assets/js/tinymce-button.min.js'; | |
return $plugin_array; | |
} |
This file contains 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 | |
public function initialize() { | |
add_filter( | |
'mce_external_plugins', | |
array( $this->loader, 'add_button' ) | |
); | |
add_filter( | |
'mce_buttons', | |
array( $this->loader, 'register_button' ) | |
); | |
} |
This file contains 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 | |
class Acme_Button_Loader { | |
// Code removed for brevity's sake | |
public function add_button( $plugin_array ) { | |
$plugin_array['yhd'] = plugin_dir_url( dirname( __FILE__ ) ) . 'admin/assets/js/tinymce-button.min.js'; | |
return $plugin_array; | |
} | |
public function register_button( $buttons ) { | |
array_push( | |
$buttons, | |
'selectcolor' | |
); | |
return $buttons; | |
} | |
} |
This file contains 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
(function() { | |
'use strict'; | |
tinymce.create( 'tinymce.plugins.acme', { | |
init: function( ed, url ) { | |
// Code removed for brevity's sake | |
// Assume this file is the path to your dependency | |
var sJavaScriptUrl = 'dependency.js'; | |
ed.addCommand( 'acmebutton', function() { | |
jQuery.getScript( sJavaScriptUrl, function() { | |
acme_init_selector( jQuery ); | |
}); | |
}); | |
}, | |
// Code removed for brevity's sake | |
})( jQuery ); |
This file contains 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
/** | |
* Retrieves the URL of the assets to be used for the button in | |
* the editor. | |
* | |
* @access private | |
* @since 1.0.0 | |
* | |
* @param string url The URL of where this JavaScript file is located | |
* @return string sUrl The path to the assets directory | |
*/ | |
_assetsUrl: function( url ) { | |
var aUrl, i, l, sUrl = ''; | |
aUrl = url.split( '/' ); | |
for ( i = 0, l = aUrl.length - 1; i < l; i++ ) { | |
sUrl += aUrl[ i ] + '/'; | |
} | |
return sUrl; | |
}, |
This file contains 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
/** | |
* Retrieves the URL of the image to be used for the button in | |
* the editor. This function assumes the image is located a directory above | |
* where this script is located and in the `img` directory. | |
* | |
* @access private | |
* @since 1.0.0 | |
* | |
* @param string url The URL of where this JavaScript file is located | |
* @return string The path to the image used for the button | |
*/ | |
_imgUrl: function( url ) { | |
return this._assetsUrl( url ) + 'img/temp.png'; | |
}, |
This file contains 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
(function( $ ) { | |
'use strict'; | |
// Used to track the number of times this plugin has been invoked | |
var iInvoked = 0; | |
tinymce.create( 'tinymce.plugins.acme', { | |
/** | |
* Initializes the plugin, this will be executed after the plugin has been created. | |
* This call is done before the editor instance has finished it's initialization | |
* so use the onInit event | |
* of the editor instance to intercept that event. | |
* | |
* @since 1.0.0 | |
* | |
* @param tinymce.Editor ed Editor instance that the plugin is initialized in. | |
* @param string url Absolute URL to where this plugin is located. | |
*/ | |
init: function( ed, url ) { | |
/** | |
* This code is used because other plugins may be calling `media_buttons` | |
* which will trigger this plugin to run. In order to make sure the plugin | |
* is running as many times as it should and no more, we have to place this | |
* check here in order to make sure it works. | |
*/ | |
iInvoked++; | |
if ( 1 < iInvoked ) { | |
return; | |
} | |
// The rest of the code that should fire appears here. | |
} | |
}); | |
tinymce.PluginManager.add( 'acme', tinymce.plugins.acme ); | |
})( jQuery ); |
This file contains 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
/** | |
* Retrieves the URL of the assets to be used for the button in | |
* the editor. | |
* | |
* @access private | |
* @since 1.0.0 | |
* | |
* @param string url The URL of where this JavaScript file is located | |
* @return string sUrl The path to the assets directory | |
*/ | |
_assetsUrl: function( url ) { | |
var aUrl, i, l, sUrl = ''; | |
aUrl = url.split( '/' ); | |
for ( i = 0, l = aUrl.length - 1; i < l; i++ ) { | |
sUrl += aUrl[ i ] + '/'; | |
} | |
return sUrl; | |
}, | |
/** | |
* Retrieves the URL of the image to be used for the button in | |
* the editor. This function assumes the image is located a directory above | |
* where this script is located and in the `img` directory. | |
* | |
* @access private | |
* @since 1.0.0 | |
* | |
* @param string url The URL of where this JavaScript file is located | |
* @return string The path to the image used for the button | |
*/ | |
_imgUrl: function( url ) { | |
return this._assetsUrl( url ) + 'img/temp.png'; | |
}, | |
init: function( ed, url ) { | |
ed.addButton( 'acmeoption', { | |
title: 'Acme Option', | |
cmd: 'acmeoption', | |
image: this._imgUrl( url ) | |
}); | |
} |
This file contains 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
ed.execCommand( | |
'mceInsertContent', | |
false, | |
'This is the content that we want to insert into the editor.' | |
); |
This file contains 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
/** | |
* Retrieves the URL of the assets to be used for the button in | |
* the editor. | |
* | |
* @access private | |
* @since 1.0.0 | |
* | |
* @param string url The URL of where this JavaScript file is located | |
* @return string sUrl The path to the assets directory | |
*/ | |
_assetsUrl: function( url ) { | |
var aUrl, i, l, sUrl = ''; | |
aUrl = url.split( '/' ); | |
for ( i = 0, l = aUrl.length - 1; i < l; i++ ) { | |
sUrl += aUrl[ i ] + '/'; | |
} | |
return sUrl; | |
}, | |
/** | |
* Retrieves the URL of the image to be used for the button in | |
* the editor. This function assumes the image is located a directory above | |
* where this script is located and in the `img` directory. | |
* | |
* @access private | |
* @since 1.0.0 | |
* | |
* @param string url The URL of where this JavaScript file is located | |
* @return string The path to the image used for the button | |
*/ | |
_imgUrl: function( url ) { | |
return this._assetsUrl( url ) + 'img/temp.png'; | |
}, | |
init: function( ed, url ) { | |
ed.addButton( 'acmeoption', { | |
title: 'Acme Option', | |
cmd: 'acmeoption', | |
image: this._imgUrl( url ) | |
}); | |
ed.execCommand( | |
'mceInsertContent', | |
false, | |
'This is the content that we want to insert into the editor.' | |
); | |
} |
This file contains 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
prompt( "What is your first name?" ); |
This file contains 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
/** | |
* Prompts the user for the first name and returns the value to the caller. | |
* | |
* @since 1.0.0 | |
* | |
* @return string The first name of the user. If not speciied, then a default string. | |
*/ | |
function get_user_name() { | |
return prompt( "What is your first name?" ); | |
} |
This file contains 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
/** | |
* Prompts the user for the first name and returns the value to the caller. | |
* | |
* @since 1.0.0 | |
* @param object $ A reference to the jQuery object | |
* @return string name The first name of the user. If not speciied, then a default string. | |
*/ | |
function get_user_name( $ ) { | |
var name; | |
// Grab the input from the user | |
name = prompt( "What is your first name?" ); | |
// If the user didn't enter any information, then provide a default value | |
if ( null === $.trim( name ) || 0 === name.length ) { | |
name = '[User provided no input.]'; | |
} | |
return name; | |
} |
This file contains 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
var name = get_user_name( $ ); | |
ed.execCommand( | |
'mceInsertContent', | |
false, | |
name | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment