How to Switch the WordPress Editor to HTML by Default

How to Switch the WordPress Editor to HTML by Default

The default WordPress WYSIWYG editor is great for ordinary posts and even for some simple code. However, it can seriously break the formatting of your article if you’re copy pasting stuff from elsewhere. Even though WordPress does a pretty good job of converting elements like headings, bullet points and tables from other formats, it still imperfect and you might find yourself having to repeatedly reformat content.

One way to ensure that your writers don’t create formatting problems is by making them type or paste their content into the “Text” portion of the WordPress editor. This way, you can be sure that there will be no special characters or formatting goof ups. It’s easy to ask your writers to switch to textmode before starting. But what if you want to make it the default instead? Why not reduce the chances of error and simply do it for them rather than risk having them forget? In this article, we take a look at how to switch the default WordPress editor to HTML instead of the standard visual interface.

Let’s take a look at how to do this globally, and then I’ll give you the code for implementing it only for specific roles.

Changing the Editor to HTML Globally

When you start writing a post with WordPress, it displays the visual editor by default for new posts and remembers your preference for individual ones so you can see in the screenshot below that the “Visual” tab is activated for my new test post.

visual editor default

If I’m the only author on this blog am a I can remove the visual editor entirely by visiting my profile options and disabling it from there as you can see in this screenshot:

disable from profile

But this is a nuclear solution. The visual editor certainly has its uses and you don’t want to remove that capability entirely. Moreover, this is an extremely use a specific solution and isn’t scalable. You need to repeat this process for each new user you create. Instead of that, let’s write some code to merely switch the default style to text. Open up your functions.php file and paste the following code before your closing ?> PHP tag:

function show_html_editor() {
return 'html';
}
add_filter( 'wp_default_editor', 'show_html_editor' );

If you don’t know how to add code to WordPress like this, take a look at my earlier tutorial. This snippet will change the default WordPress editor to “Text” sitewide. It won’t remove the visual option and you can switch back to it at any time.

text mode default

While this may be what you’re looking for, what if you need to make this change only for a specific category of users? Let’s say only for those with editing privileges. In which case, we simply check whether or not the current user has a specific capability and change the editor if true. Here’s the code to do that:

function show_html_editor() {

if (current_user_can('edit_posts'))
return 'html';
else
return 'tinymce';
}
add_filter( 'wp_default_editor', 'show_html_editor' );

The line in bold shows us what we’re checking for. You can include any user capability here. If you want, here is a complete list of capabilities and their names included by default with WordPress. Simply replace “edit_posts” with the one you want.

This code can be modified to accommodate a variety of conditions. You can selectively switch to the text editor for different page templates or even specific users instead of a capability-based rule. To do the latter, use the following code:

function show_html_editor() {
global $current_user;
get_currentuserinfo();
if ($current_user->ID == 2)
return 'html';
else
return 'tinymce';
}
add_filter( 'wp_default_editor', 'show_html_editor' );

Where “2” is the ID of the user we’re targeting. Simply change this number accordingly. Using these techniques, you can say goodbye to all your formatting errors and copy paste issues.

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.