1

Custom quicktags not working after Wordpress 6.0

wp 5.8.x/5.9.x: working -- wp 6.0: not working

Buttons are not showing here: https://i.stack.imgur.com/aUXyV.jpg

Console error: Uncaught ReferenceError: QTags is not defined

I am using this code.

    function my_quicktags() {

    if ( wp_script_is( 'quicktags' ) ) {
    ?>
    <script type="text/javascript">
    QTags.addButton( 'eg_php', 'PHP', '<pre><code class=\"language-php\">', '</code></pre>', 'p', 'PHP Code', 200 );
    QTags.addButton( 'eg_css', 'CSS', '<pre><code class=\"language-css\">', '</code></pre>', 'q', 'CSS Code', 201 );
    QTags.addButton( 'eg_html', 'HTML', '<pre><code class=\"language-html\">', '</code></pre>', 'r', 'HTML Code', 202 );
    QTags.addButton( 'eg_callback', 'CSS div', css_callback );

    function css_callback(){
        var css_class = prompt( 'Class name:', '' );

        if ( css_class && css_class !== '' ) {
            QTags.insertContent('<div class="' + css_class +'"></div>');
        }
    }
    </script>
    <?php
    }

}
add_action( 'admin_print_footer_scripts', 'my_quicktags' );
6
  • 2
    What’s not working? Are the buttons not working? Or do they not appear? Are there any errors in the console? Please elaborate. May 26, 2022 at 17:55
  • 1
    can you be more specific? E.g. they're added but the buttons do nothing? Or there's a JS error in the console? Where are you trying to use these? Is there a particular screen you're trying to use this on? Have you confirmed your code runs and that it isn't broken by an error somewhere else in the browsers dev console?
    – Tom J Nowell
    May 26, 2022 at 17:57
  • I tested out QTags.addButton( 'eg_php', 'PHP', '<pre><code class=\"language-php\">', '</code></pre>', 'p', 'PHP Code', 200 ); in WP 6.0 and it worked fine, I notice though, that rather than adding an inline script to quicktags, you're using wp_script_is and then manually writing out a script tag, which is highly unusual and non-standard. Are you sure that if ( wp_script_is( 'quicktags' ) ) { actually works as intended? Was there a particular reason you chose to write it this way? I suspect this has always been broken but it worked out of luck
    – Tom J Nowell
    May 26, 2022 at 18:01
  • Buttons are not showing here: imgur.com/a/T05o0WX Console error: Uncaught ReferenceError: QTags is not defined
    – Dominator0
    May 26, 2022 at 18:32
  • 1
    @PatJ ticket opened to fix it at github.com/WordPress/Documentation-Issue-Tracker/issues/357
    – Tom J Nowell
    May 27, 2022 at 15:32

6 Answers 6

3

I had the same problem. I fixed it by using the ep_enqueue_script aproach shown here: https://codex.wordpress.org/Quicktags_API listed under "A more modern example".

It's the same thing that Tom mentions. Instead of using 'admin_print_fotter_scripts' to write an inline script on the page, the example shows putting the Qtags javascript in a separate JS file, and loading it with 'admin_enqueue_scripts' as the action and calling wp_enqueue_script.

The Quicktags_API link above shows both methods, but the inline script method stoped working for me.

3

Found a fix.

    <script type="text/javascript">
           window.onload=function(){ 


    }
</script>

make sure the JS loads after the page fully loads this will allow the qtags js to work. Had the same issue as you right after I updated to 6.0. This fixed it for me.

2
  • For me this is the best solution.
    – Kifsif
    Jul 26, 2022 at 15:53
  • I can confirm that simply adding the window.onload=function() worked for me to, it restored my custom quick tag buttons, I will post a fuller example below in case anyone else needs a bit more help.
    – Trisha
    May 18 at 22:30
2

You can use wp_add_inline_script to add more quicktags, ensuring it only runs when quicktags is used, and runs after it's loaded:

function add_quicktag_paragraph() {
    wp_add_inline_script(
        'quicktags',
        "QTags.addButton( 'eg_paragraph', 'p', '<p>', '</p>', 'p',     'Paragraph tag', 1 );"
);
add_action( 'wp_enqueue_scripts', 'add_quicktag_paragraph' );

Here the second parameter of wp_add_inline_script gets put into a <script> tag by WordPress, and it places it so that it shows after quicktags is loaded, not before. I've places a single line of code in my example but you could insert multiple lines instead.

This solves several problems the original snippet in the handbook had:

  • it works on non-admin pages that use quicktags
  • it always runs the code after quicktags is loaded, not before.
  • no hardcoded script tags
  • it's possible to filter and process this because of wp_add_inline_script
  • if a plugin removes the quicktags code then this will not show and break

Another alternative is to write a JS file with your quicktags additions, and enqueue it, declaring quicktags as a dependency. This ensures it is always loaded in the correct order.

3
  • Thanks but can you post an example using <script type="text/javascript"> ... </script> ?
    – Dominator0
    May 28, 2022 at 1:41
  • @Dominator0 that would involve completely destroying my answer and rewriting it using something that runs into your original problem, hardcoded script tags aren't necessary here, but also wp_add_inline_script is creating those script tags for you, the second parameter to wp_add_inline_script is what's going to go inside the script tags. In my example i have a single line of javascript but you could put hundreds of lines of javascript.
    – Tom J Nowell
    May 29, 2022 at 19:36
  • e.g. wp_add_inline_script( 'quicktags', 'your javascript goes here' );
    – Tom J Nowell
    May 29, 2022 at 19:38
1

Another fix..

   <script type="text/javascript">
    window.addEventListener('DOMContentLoaded', () => {
      QTags.addButton( 'test', 'Test', '<test>', '</test>', 'test' );
    });
    </script>
0

This work for me (more info)

My QTags:

QTags.addButton('codelights', 'CodeLights', btnAction);

I edit to:

window.onload = function() {
    QTags.addButton('codelights', 'CodeLights', btnAction);
};
0

I had the same problem, my custom quicktags I use in the Classic Editor (Text tab) disappeared after updating to 6.0+ and after much research I can confirm that the old code DOES still work with the addition of a single line of script (a window event) as suggested above by @kegnum and @Dominator0 (thanks!).

HERE is the full code snippet in case anyone needs it, be sure to modify this to suit your OWN needs, here I am adding some H tags:

// add more buttons to the html editor
function twe_add_quicktags() {
if (wp_script_is('quicktags')){
?>
<script type="text/javascript">
window.addEventListener('load', function(){
QTags.addButton( 'eg_heading_3', 'h3', '<h3>', '</h3>', 'h3', 'Heading 3 tag', 1 );
QTags.addButton( 'eg_heading_4', 'h4', '<h4>', '</h4>', 'h4', 'Heading 4 tag', 1 );
QTags.addButton( 'eg_heading_5', 'h5', '<h5>', '</h5>', 'h5', 'Heading 5 tag', 1 );
});
</script>
<?php
}
}
add_action( 'admin_print_footer_scripts', 'twe_add_quicktags' );

I know there are several other methods, but this is a very simple one and it does the trick.

Your Answer

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

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