Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Do you mean shortcode? If so, shortcodes run server-side. –  Brian Fegter Feb 6 '12 at 23:02
    
Can you provide a better example of what you're trying to do? More of a usage case than anything really... –  m0r7if3r Feb 6 '12 at 23:46
    
Sure, I'll expand the question. –  dkretz Feb 7 '12 at 0:17

2 Answers 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);
    };
share|improve this answer
    
What are the callback arguments e, c, and ed? –  dkretz Sep 16 '12 at 6:55
    
@le dorfier Sorry they are not needed, I removed it from my post –  jasondavis Sep 17 '12 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 Sep 17 '12 at 4:45
    
I have added a new example that uses them –  jasondavis Sep 17 '12 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 Aug 22 '13 at 21:31

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

share|improve this answer
    
I suspect they only apply to the TinyMCE editor interface. quicktags are for the html editor interface. –  dkretz Feb 7 '12 at 0:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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