How to Switch by Default the WordPress Editor to HTML

How to Switch by Default the WordPress Editor to HTML
How to Switch by Default the WordPress Editor to HTML

Exthemes Dev Blog – The default WordPress Editor WYSIWYG 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 WordPress 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.

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' );

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' );

That’s it.

I think the post will be helpful for you and save your time.

Happy Coding.

Credits:
webhostinghero.org

Howdy, I’m exthemes. I’m a web developer living in indonesia. I am a fan of web development, seo, and php. I’m also interested in wordpress and web designer.

You May Like These Posts

No Comments