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.
Via your theme’s functions.php add these codes for disabling one or all.
for widgets only
// Disable Editor in the Text Widget
add_action( 'admin_init', function() {
global $wp_scripts;
if ( $wp_scripts ) {
$wp_scripts->remove( 'text-widgets' );
}
});
// Style the clean textarea for better padding and readability
add_action('admin_print_styles-widgets.php', function() {
echo '<style>
.widefat[id*="text"] {
padding: 15px !important;
line-height: 1.5 !important;
background: #fff !important;
border: 1px solid #ccc !important;
box-sizing: border-box !important;
}
</style>';
});
add_action('in_widget_form', function( $widget, $return, $instance ) {
if ( $widget->id_base == 'text' ) {
$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>
<?php
}
}, 10, 3);
for posts & pages
// Disable Visual Tab for Posts and Pages
add_filter('user_can_richedit', '__return_false');
for all (widgets, posts, pages…)
// Disable the visual editor (TinyMCE) globally
add_filter('user_can_richedit', '__return_false');
force to start posts, pages, widgets in Code mode
// Force 'html' (Code) as the default editor mode globally
add_filter('wp_default_editor', function() {
return 'html';
});
And that’s done!