WordPress: Enable paste as text in Classic Editor and remove HTML when inserting

Date: 
29. January 2024
Author: 
mbs
Category: 

If you copy and paste from websites/word documents to add post and pages in WordPress, then you maybe have noticed that you are copying HTML when you insert.

Yes, there is a key on both Windows and Mac, that can remove the HTML format, but if you forget this, the mess is already being added, and you have moved on with your page so you totally forgot.

Here is an option to disable all format when you paste in the classic editor.

This is only working on the classic editor at the moment

/**
 * Enable paste as text in Classic Editor and remove HTML when inserting
 */
add_filter('tiny_mce_before_init', function ($init) {
    $init['paste_as_text'] = true;
    return $init;
});

add_filter('wpuf_textarea_editor_args', function ($args) {
    $args['tinymce']['plugins'] = 'paste';
    $args['tinymce']['paste_as_text'] = true;
    return $args;
});

add_action('wp_footer', 'custom_paste_as_text', 9999);
add_action('admin_head', 'custom_paste_as_text', 9999);

function custom_paste_as_text()
{
    if (is_user_logged_in() && ((isset($_GET['et_fb']) && $_GET['et_fb'] === '1') || (isset($_GET['page']) && $_GET['page'] === 'et_theme_builder'))) {
        ?>
        <script>
            document.addEventListener('DOMContentLoaded', function () {
                tinymce.on("AddEditor", function (e) {
                    setTimeout(function () {
                        plain_text(e);
                    }, 300);

                    setTimeout(function () {
                        plain_text(e);
                    }, 1000);

                    setTimeout(function () {
                        plain_text(e);
                    }, 2000);

                    function plain_text(e) {
                        try {
                            if (e.editor.plugins.paste.clipboard.pasteFormat.get() !== 'text') {
                                e.editor.execCommand("mceTogglePlainTextPaste");
                                var notificationButton = document.querySelector('.mce-notification button');
                                if (notificationButton) {
                                    notificationButton.click();
                                }
                            }
                        } catch (exception) {
                            // Prevent JS error originating from execCommand above when tinymce does not have NotificationManager
                        }
                    }
                });
            });
        </script>
        <?php
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

© Copyright 2007-2024
mbsTECH.dk

Build with
and 
Oxygenbuilder
I'm using 
Ahoy