0

In short, I want to use the default_content filter to return slightly different content depending on if it will be used in the block editor or not (eg. the quicktags or tinymce editor) - is there any way I can tell from the default_content filter how the content will be used?

Lengthy version: I created a simple author signature (simple text field added via acf) that adds a signature to the bottom of a user's posts/comments via the default_content filter (ie. it adds it to the editor, so could be changed or removed). This worked fine in the classic editor, but not in the block editor, which by default puts that in a classic editor block. To solve that I made an author signature block, which works nicely for the block editor, and converts perfectly to the classic editor.

The issue I face is that in the classic editor I need to add an additional line break ('
') ahead of the signature, so it's clear where you should begin typing. (Without it you have to click on the on the signature line, hit enter, then move the cursor up.) If I leave that line break in for the block editor, it works wrong (the line break is turned into an html block with empty paragraph tags).

I have seen various ways to try to guess if the block editor or classic editor are in use on the site, but in this case they both are. Eg. I will edit a post with block editor, but comments use the classic editor. I have found a few hooks that are triggered for one editor or the other, but they all seem to run too late to be useful (after default_content).

So far I haven't found any way to determine what needs to be output from default_content, any thoughts?

0

Looks like WP_Screen::is_block_editor() is what you want. Here's some rough code to demonstrate:

add_filter( 'default_content', function( $content, $post ) {
    $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
    $is_block_editor = $screen ? $screen->is_block_editor() : false;

    if ( $is_block_editor ) {
        // Do your block editor stuff here
    } else {
        // Do your classic editor stuff here
    }

    return $content;
}, 10, 2 );

If you only want this to occur for a specific post-type, it would look more like this:

add_filter( 'default_content', function( $content, $post ) {
    if ( ! empty( $post->post_type ) && 'your_post_type' === $post->post_type ) {

        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
        $is_block_editor = $screen ? $screen->is_block_editor() : false;

        if ( $is_block_editor ) {
            // Do your block editor stuff here
        } else {
            // Do your classic editor stuff here
        }
    }

    return $content;
}, 10, 2 );
  • I don't know how I missed that in my searching, but is_block_editor() works perfectly for my use case, thank you. – Jesse Norell Sep 19 at 17:17

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.