how can default_content filter tell if content will load in block editor or classic editor?

In short, I want to use the default_content filter to return slightly different content depending on if it will be used

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?

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?

Share Improve this question asked Sep 17, 2019 at 0:53 Jesse NorellJesse Norell 937 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745152922a4613944.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信