I found this code to functions.php

add_filter( 'wp_default_editor', create_function('', 'return "html";') );

but I need to limit it to a specific set of roles (such as Editor or Author).

How do I do that?



create_function() is deprecated as of PHP 7.2.0, so you should avoid using it. Instead, use an anonymous function.

Additionally, it's better the check the current user role inside the callback function, so that the current user role is checked when the filter is applied, not when the hook is added, which could be before the current user role can be determined.

    function( $default_editor ) {
        if ( current_user_can( 'editor' ) || current_user_can( 'author' ) ) {
            $default_editor = 'html';

        return $default_editor;

This should do it for you.

if ( current_user_can( 'editor' ) || current_user_can( 'author' ) ) {
    add_filter( 'wp_default_editor', create_function( '', 'return "html";' ) );

Basically what you're doing is asking if the current user is an editor or an author before you add the filter.

As you mentioned, you essentially put this code into your functions.php file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.