user_can_richedit

Home » Hooks » user_can_richedit

The WordPress hook user_can_richedit is a filter hook that is used to determine whether the rich text editor should be enabled for a user. It is triggered when a user is about to edit a post or page, and it allows developers to modify whether or not the user should have access to the rich text editor.

The rich text editor is a WYSIWYG editor that allows users to format text without having to use HTML code. By default, WordPress enables the rich text editor for users who have the unfiltered_html capability. However, developers can use the user_can_richedit hook to modify this behavior and enable or disable the rich text editor for specific users or roles.

The user_can_richedit hook takes two parameters: $default and $user_id. The $default parameter is a boolean value that indicates whether the rich text editor should be enabled or disabled. The $user_id parameter is the ID of the user attempting to edit the post or page.

Example usage:

function disable_rich_text_editor_for_subscribers( $default, $user_id ) {
    $user = get_userdata( $user_id );
    if ( in_array( 'subscriber', $user->roles ) ) {
        return false;
    }
    return $default;
}
add_filter( 'user_can_richedit', 'disable_rich_text_editor_for_subscribers', 10, 2 );

In the example above, the disable_rich_text_editor_for_subscribers function is used to disable the rich text editor for users with the subscriber role. The function checks whether the user is a subscriber, and if so, it returns false to disable the rich text editor. If the user is not a subscriber, the function returns $default to preserve the default behavior. The add_filter function is used to attach the disable_rich_text_editor_for_subscribers function to the user_can_richedit hook.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.