首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Gutenberg编辑器中加载之前过滤post_content

在Gutenberg编辑器中加载之前过滤post_content
EN

WordPress Development用户
提问于 2020-05-20 15:54:56
回答 1查看 660关注 0票数 1

我试图在编辑器中显示编辑器内容(块)之前对其进行操作。因此,理想情况下,我将执行搜索和替换,编辑器将有一个修改版本的块,但不会保存,直到用户单击“保存”。

是否有邮件内容的过滤器?

我认为这件事应该休息一下,但我没办法找到它。

任何帮助都是非常感谢的。

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2020-05-29 12:54:35

我发现,在加载数据时,无法更改编辑器中的数据。但在那之后就有可能替换数据。

JS: script.js

代码语言:javascript
运行
复制
wp.domReady(function () {
    const newData = window.myNewBlocksData;

    if (newData) {
        wp.data.dispatch('core/block-editor').resetBlocks(wp.blocks.parse(newData));
        console.log('replaced');
    }
}) 

PHP:

代码语言:javascript
运行
复制
is_block_editor) &&
            !empty($screen->base) &&
            $screen->base === 'post'
        )) {
            return;
        }

        $currentPost = get_post();
        $alreadyManipulated = get_post_meta($currentPost->ID, "{$this->prefix}_data_is_modified", true);

        if (!empty($alreadyManipulated)) {
            return;
        }

        $newData = wp_slash($this->modifyData());

        wp_add_inline_script('wp-editor', "var myNewBlocksData=`{$newData}`");

        wp_enqueue_script(
            'my-data-manipulation',
            'path/to/my/script.js',
            ['wp-editor'],
            false,
            true
        );
    }

    public function modifyData()
    {
        $currentPost = get_post();
        $content = $currentPost->post_content;

        return preg_replace_callback("", function ($matches) {
            if (!empty($matches[2])) {
                $array = self::decode_block_attributes($matches[2]);


                // ...
                // Do something with this $array ...
                // ...


                $block = new WP_Block_Parser_Block($matches[1], $array, [], '', []);

                $serialized = serialize_block((array)$block);

                $serialized = trim($serialized, '<>');

                return $serialized;
            }

            return "<$matches[0]>";
        }, $content);
    }

    public static function is_json($item)
    {
        if (is_string($item)) {
            $json = json_decode($item, true);

            return is_array($json) && json_last_error() == JSON_ERROR_NONE;
        }

        return false;
    }

    public static function decode_block_attributes($item)
    {
        if (self::is_json($item) || is_array($item)) {
            $json = is_array($item) ? $item : json_decode($item, true);

            return $json;
        }

        return $item;
    }

    public function markPostManipulationDone($postId, $post, $update)
    {
        if (
            !$update ||
            $post->post_type === 'revision' ||
            !current_user_can('edit_post', $postId)
        ) {
            return $postId;
        }

        return update_post_meta($postId, "{$this->prefix}_data_is_modified", current_time('U'));
    }
}

new MyBlocksManipulation(); // Somwhere on top, before the theme or plugin content is rendered.
票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/367128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档