Codex tools: Log in
Languages: English • Bahasa Indonesia • 中文(简体) • (Add your language)
Contents |
The Quicktags API allows you to include additional buttons in the Text (HTML) mode of the WordPress editor.
This page was proposed on the WP DEVEL blog here: http://wpdevel.wordpress.com/2011/12/07/whats-new-javascript-in-3-3/ And the relevant Trac ticket is http://core.trac.wordpress.org/ticket/16695
QTags.addButton( id, display, arg1, arg2, access_key, title, priority, instance );
// add more buttons to the html editor
function appthemes_add_quicktags() {
?>
<script type="text/javascript">
QTags.addButton( 'eg_paragraph', 'p', '<p>', '</p>', 'p', 'Paragraph tag', 1 );
QTags.addButton( 'eg_hr', 'hr', '<hr />', '', 'h', 'Horizontal rule line', 201 );
QTags.addButton( 'eg_pre', 'pre', '<pre lang="php">', '</ pre>', 'q', 'Preformatted text tag', 111 );
</script>
<?php
}
add_action( 'admin_print_footer_scripts', 'appthemes_add_quicktags' );
(Note: We purposely added a space in </ pre> so the example code above would render correctly. You’ll need to remove it in order for the tag to work in your code.)
The above would add HTML buttons to the default Quicktags in the Text editor. For example, the "p" button HTML would be:
<input type="button" id="qt_content_eg_paragraph" accesskey="p" class="ed_button" title="Paragraph tag" value="p">
(The ID value for each button is automatically prepended with the string 'qt_content_'.)
Here is a dump of the docblock from quicktags.js, it's pretty useful on it's own.
/**
* Main API function for adding a button to Quicktags
*
* Adds qt.Button or qt.TagButton depending on the args. The first three args are always required.
* To be able to add button(s) to Quicktags, your script should be enqueued as dependent
* on "quicktags" and outputted in the footer. If you are echoing JS directly from PHP,
* use add_action( 'admin_print_footer_scripts', 'output_my_js', 100 ) or add_action( 'wp_footer', 'output_my_js', 100 )
*
* Minimum required to add a button that calls an external function:
* QTags.addButton( 'my_id', 'my button', my_callback );
* function my_callback() { alert('yeah!'); }
*
* Minimum required to add a button that inserts a tag:
* QTags.addButton( 'my_id', 'my button', '<span>', '</span>' );
* QTags.addButton( 'my_id2', 'my button', '<br />' );
*/
Here are the values of the default Quicktags added by WordPress to the Text editor (table sorted by access key value). Access key and ID must be unique. When adding your own buttons, do not use these values:
| Accesskey | ID | Value | Tag Start | Tag End |
|---|---|---|---|---|
| a | link | link | <a href="' + URL + '"> | </a> |
| b | strong | b | <strong> | </strong> |
| c | code | code | <code> | </code> |
| d | del | del | <del datetime="' + _datetime + '"> | </del> |
| f | fullscreen | fullscreen | ||
| i | em | i | <em> | </em> |
| l | li | li | \t<li> | </li>\n |
| m | img | img | <img src="' + src + '" alt="' + alt + '" /> | |
| o | ol | ol | <ol>\n | </ol>\n\n |
| q | block | b-quote | \n\n<blockquote> | </blockquote>\n\n |
| s | ins | ins | <ins datetime="' + _datetime + '"> | </ins> |
| t | more | more | <!--more--> | |
| u | ul | ul | <ul>\n | </ul>\n\n |
| spell | lookup | |||
| close | close |
(Some tag values above use variables, such as URL and _datetime, passed from functions.)
Since: 3.3
qt.addButton() is located in wp-includes/js/quicktags.js and wp-includes/js/quicktags.min.js.