Skip to content

Instantly share code, notes, and snippets.

@petrozavodsky
Forked from mattyrob/qtags.php
Created October 15, 2015 08:24
Show Gist options
  • Save petrozavodsky/c91c3eeece4ee639c28f to your computer and use it in GitHub Desktop.
Save petrozavodsky/c91c3eeece4ee639c28f to your computer and use it in GitHub Desktop.
Quicktags API Test
<?php
/*
Plugin Name: Quicktags API Test
Description: Testing a few different kinds of Quicktags buttons
Author: Kailey Lampert
Author URI: http://kaileylampert.com/
*/
add_action( 'admin_print_footer_scripts', 'a_few_different_quicktags_buttons', 100 );
function a_few_different_quicktags_buttons() {
?>
<script type="text/javascript">
QTags.addButton( 'k_01', 'test 1', prompt_user );
function prompt_user(e, c, ed) {
prmt = prompt('Enter something');
if ( prmt === null ) return;
rtrn = '[short]' + prmt + '[/short]';
this.tagStart = rtrn;
QTags.TagButton.prototype.callback.call(this, e, c, ed);
}
QTags.addButton( 'k_02', 'test 2', simple_alert );
function simple_alert(e, c, ed) {
alert('hello');
}
QTags.addButton( 'k_03', 'test 3', '[selfclosing/]');
QTags.addButton( 'k_04', 'test 4', '[bookend]', '[/bookend]' );
// Add a button called 'abbr' with a callback function
QTags.addButton( 'abbr', 'Abbr', abbr_prompt );
// and this is the callback function
function abbr_prompt(e, c, ed) {
var prmt, t = this;
if ( ed.canvas.selectionStart !== ed.canvas.selectionEnd ) {
// if we have a selection in the editor define out tagStart and tagEnd to wrap around the text
// prompt the user for the abbreviation and return gracefully on a null input
prmt = prompt('Enter Abbreviation');
if ( prmt === null ) return;
t.tagStart = '<abbr title="' + prmt + '">';
t.tagEnd = '</abbr>';
} else if ( ed.openTags ) {
// if we have an open tag, see if it's ours
var ret = false, i = 0, t = this;
while ( i < ed.openTags.length ) {
ret = ed.openTags[i] == t.id ? i : false;
i ++;
}
if ( ret === false ) {
// if the open tags don't include 'abbr' prompt for input
prmt = prompt('Enter Abbreviation');
if ( prmt === null ) return;
t.tagStart = '<abbr title="' + prmt + '">';
t.tagEnd = false;
if ( ! ed.openTags ) {
ed.openTags = [];
}
ed.openTags.push(t.id);
e.value = '/' + e.value;
} else {
// otherwise close the 'abbr' tag
ed.openTags.splice(ret, 1);
t.tagStart = '</abbr>';
e.value = t.display;
}
} else {
// last resort, no selection and no open tags
// so prompt for input and just open the tag
prmt = prompt('Enter Abbreviation');
if ( prmt === null ) return;
t.tagStart = '<abbr title="' + prmt + '">';
t.tagEnd = false;
if ( ! ed.openTags ) {
ed.openTags = [];
}
ed.openTags.push(t.id);
e.value = '/' + e.value;
}
// now we've defined all the tagStart, tagEnd and openTags we process it all to the active window
QTags.TagButton.prototype.callback.call(t, e, c, ed);
};
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment