WordPress: Disable Formatting in WordPress Classic Editor (Paste in plain text automatic)

Date: 
9. January 2024
Author: 
mbs
Category: 

Here is a simply code inspired by: https://wordpress.org/plugins/paste-as-plain-text-by-default/

But this is a modified version more simply, and can fit you theme functions, or in code snippet so you have control over it.

Here is the code

/**
 * Enable paste as text in Classic Editor
 */
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 
by Michael Bay Sørensen
WebGPT.ninja
Build with
and 
Oxygenbuilder
I'm using 
Ahoy