1

I want to add quicktags that do more than just add markup. For instance, I would like to add a quicktag to run a function do apply a js regex on the page content. How can I add a quicktag that executes a function?

To give a specific example, I would like to add a button that removes all markup tags in the entire post text. This is analogous to the predefined quicktags for finding unmatched tags, or "proofread", etc.

3
  • Do you mean shortcode? If so, shortcodes run server-side. Commented Feb 6, 2012 at 23:02
  • Can you provide a better example of what you're trying to do? More of a usage case than anything really...
    – mor7ifer
    Commented Feb 6, 2012 at 23:46
  • Sure, I'll expand the question.
    – dkretz
    Commented Feb 7, 2012 at 0:17

2 Answers 2

2

Simple example that will add a Quicktag button that calls a Javascript function when it is clicked...

<?PHP
function _add_my_quicktags(){ ?>
    <script type="text/javascript">
    QTags.addButton( 'alert', 'Alert Button', simple_alert );

        function simple_alert() { 
            alert('hello, example callback function');
        }
    </script>
<?php 
}
add_action('admin_print_footer_scripts',  '_add_my_quicktags');
?>

UPDATE.... Adding a Callback function that utilizes the passed in variables...

    // 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);
    };
5
  • What are the callback arguments e, c, and ed?
    – dkretz
    Commented Sep 16, 2012 at 6:55
  • @le dorfier Sorry they are not needed, I removed it from my post
    – JasonDavis
    Commented Sep 17, 2012 at 1:12
  • But they might be useful, if they include the textarea content, selected text, etc. Something analogous to a shortcode but without a host trip and js instead of php.
    – dkretz
    Commented Sep 17, 2012 at 4:45
  • I have added a new example that uses them
    – JasonDavis
    Commented Sep 17, 2012 at 6:04
  • Thanks for this answer, but a open question from my side and I hope for a answer. I like it to use var inside the function addButton()to create the strings inside the callback function dynamicly, like the string on prmt = prompt('Enter Abbreviation');. Can you explaine this?
    – bueltge
    Commented Aug 22, 2013 at 21:31
0

you should write a tinymce plugin, then load it (using tinymce advanced plugin)

1
  • I suspect they only apply to the TinyMCE editor interface. quicktags are for the html editor interface.
    – dkretz
    Commented Feb 7, 2012 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.