Mar 11, 2021  

Do you need to add an HTML code to your widget, post or page but in Visual Mode you cannot?
For widgets, sometimes “Custom HTML” creates some problems, and you can use “Text” widget but it has a view mode that won’t accept HTML.
So here’s a quick fix for removing that part entirely.

Method 1: Remove the Visual Mode from posts, pages and widgets

Under your User Profile, select “Disable the visual editor when writing” (see the attachement) and it will disable the visual mode in your entire site.
This won’t affect other users to use it, if your site has registration mode.

Method 2: Remove from widgets only

1. Via WordPress Files.
*** PLEASE NOTE: this will need to be repeated every time you upgrade WordPress Script.

Go into wp-includes/widgets and locate the file class-wp-widget-text.php

Find this line and remove it entirely
// Legacy mode when not in visual mode.
if ( isset( $instance['visual'] ) ) {
return ! $instance['visual'];
}

Additionally, if you wish to remove the annoying Custom HTML usage message of the widget in the same file, find this div and delete it and anything it’s wrapped inside it
<div class="notice inline notice-info notice-alt"> </div>

2. Via Theme Function.
*** PLEASE NOTE: this will need to be repeated every time you change your theme.
(thanks to Alex for giving me the input to work this out)

Go to your theme functions.php and at the bottom (before ” ?> ” if present)

// Disable Editor in the Text Widget
if ( $wp_version >= 4.8 )
{
add_action( 'admin_init', function()
{
global $wp_scripts;
if ( $wp_scripts )
{
$wp_scripts->remove( 'text-widgets' );
}
});
add_action('in_widget_form', function( $widget, $return, $instance )
{
if ($widget->id_base=='text')
{
$filter = isset( $instance['filter'] ) ? $instance['filter'] : 0;
$title = isset( $instance['title'] ) ? sanitize_text_field( $instance['title'] ) : '';
$text = isset( $instance['text'] ) ? esc_textarea( $instance['text']) : '';
?>
<p>
<label for="<?php echo $widget->get_field_id('title'); ?>"><?php esc_html_e( 'Title' ); ?>:</label>
<input class="widefat" id="<?php echo $widget->get_field_id('title'); ?>" name="<?php echo $widget->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
</p>
<p>
<label for="<?php echo $widget->get_field_id('text'); ?>"><?php esc_html_e( 'Content' ); ?>:</label>
<textarea class="widefat" rows="16" cols="20" id="<?php echo $widget->get_field_id('text'); ?>" name="<?php echo $widget->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
</p>
<p>
<input id="<?php echo $widget->get_field_id('filter'); ?>" name="<?php echo $widget->get_field_name('filter'); ?>" type="checkbox"<?php checked( $filter ); ?> class="checkbox onoff"/>
<label for="<?php echo $widget->get_field_id('filter'); ?>"><?php esc_html_e( 'Automatically add paragraphs' ); ?></label>
</p>
<?php
}
}, 10, 3);
add_filter('widget_update_callback', function( $instance, $new_instance, $old_instance, $widget )
{
if ( $widget->id_base == 'text' )
{
$instance['filter'] = ! empty( $new_instance['filter'] );
}
return $instance;
}, 10, 4);
}

And that’s done!