You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让WordPress经典/区块编辑器加载自定义字段内容?

问题:让WordPress编辑器加载自定义字段中的内容

我已将WordPress中wp_posts > post_content的数据转移至名为post_content的自定义字段,并清空了默认表中的对应内容(迁移实现类见下文)。现在需要实现让经典编辑器或区块编辑器加载该自定义字段中的内容,而非默认wp_posts表的内容。

我先后尝试了以下3个钩子(不同时使用),但均未成功:

尝试1:edit_form_after_title 钩子

add_action('edit_form_after_title', [ $this, 'load_custom_post_content_editor' ]);
    
public function load_custom_post_content_editor($post) {
        // Check if the post type is 'post' or 'page'
        if (in_array($post->post_type, array('post', 'page'))) {
            $custom_post_content = get_post_meta($post->ID, 'post_content', true);
            if (!empty($custom_post_content)) {
                $post->post_content = $custom_post_content;
            }
        }
    }

尝试2:wp_insert_post_data 钩子

add_action('wp_insert_post_data', [ $this, 'modify_post_data' ], 10, 2);

public function modify_post_data($data, $postarr) {
        // Check if the post type is allowed.
        if (in_array($data['post_type'], self::$allowed_post_types)) {
            $custom_post_content = get_post_meta($postarr['ID'], 'post_content', true);
            if (!empty($custom_post_content)) {
                $data['post_content'] = $custom_post_content;
            }
        }
        return $data;
    }

尝试3:block_editor_content 钩子

add_filter('block_editor_content', [ $this, 'filter_block_editor_content' ], 10, 2);

public function filter_block_editor_content($content, $post) {
        // Check if the post type is allowed.
        if ($post && in_array($post->post_type, self::$allowed_post_types)) {
            $custom_post_content = get_post_meta($post->ID, 'post_content', true);
            if (!empty($custom_post_content)) {
                $content = $custom_post_content;
            }
        }
        return $content;
    }

尽管post_content自定义字段中存在区块编辑器内容代码,但上述方法都无法让编辑器加载该字段内容。以下是我实现内容迁移的完整可用类:

<?php
/**
 * Plugin Name: WP JSON Search
 * Description: The WP JSON Search plugin allows you to search your WordPress website by merging complete post content, custom fields, and taxonomies into a single JSON string, which is then stored in your post_content in wp_posts table.
 * Version: 1.0
 * Author: Josh Cranwell
 * Author URI: https://github.com/joshmoto
 */

class WP_JSON_Search {

    /**
     * @var array|string[] Allowed post types to save json search content.
     */
    private static array $allowed_post_types = ['post', 'page'];

    /**
     * WP_JSON_Search constructor.
     */
    public function __construct () {

        // Save post_content to custom meta field when saving a draft or publishing a post/page.
        add_action('save_post', [ $this, 'save_post_content' ], 10, 2);

        // Filter the content to get data from the custom meta field.
        add_filter('the_content', [ $this, 'filter_the_content' ]);

    }

    /**
     * Save post_content to custom meta field when saving a draft or publishing a post/page.
     * @param $post_id
     * @param $post
     * @return void
     */
    public function save_post_content($post_id, $post): void
    {

        // Check if the post type is allowed.
        if (in_array($post->post_type, self::$allowed_post_types)) {

            // Set the post_content to the custom meta field.
            $post_content = $post->post_content;
            update_post_meta($post_id, 'post_content', $post_content);
            $post->post_content = ''; // Empty the default post_content field.

            // Unhook this function so it doesn't loop infinitely.
            remove_action('save_post', [ $this, 'save_post_content' ]);

            // Update the post, which calls save_post again.
            wp_update_post($post);

            // Re-hook this function.
            add_action('save_post', [ $this, 'save_post_content' ]);

        }
    }

    /**
     * Filter the content to get data from the custom meta field.
     * @param $content
     * @return mixed
     */
    public function filter_the_content($content): mixed
    {

        // Get the current post.
        global $post;

        // Check if the post type is allowed.
        if ($post && in_array($post->post_type, self::$allowed_post_types)) {

            // Get the post_content from the custom meta field.
            $custom_post_content = get_post_meta($post->ID, 'post_content', true);
            if (!empty($custom_post_content)) {
                $content = $custom_post_content;
            }
        }

        // Return the content.
        return $content;
    }

}

new WP_JSON_Search();

可行解决方案

要让编辑器正确加载自定义字段的内容,需要针对区块编辑器和经典编辑器分别处理,同时覆盖REST API的内容返回:

修改后的完整类代码

<?php
/**
 * Plugin Name: WP JSON Search
 * Description: The WP JSON Search plugin allows you to search your WordPress website by merging complete post content, custom fields, and taxonomies into a single JSON string, which is then stored in your post_content in wp_posts table.
 * Version: 1.0
 * Author: Josh Cranwell
 * Author URI: https://github.com/joshmoto
 */

class WP_JSON_Search {

    /**
     * @var array|string[] Allowed post types to save json search content.
     */
    private static array $allowed_post_types = ['post', 'page'];

    /**
     * WP_JSON_Search constructor.
     */
    public function __construct () {

        // Save post_content to custom meta field when saving a draft or publishing a post/page.
        add_action('save_post', [ $this, 'save_post_content' ], 10, 2);

        // Filter frontend content to load from custom meta.
        add_filter('the_content', [ $this, 'filter_the_content' ]);

        // 处理区块编辑器内容
        add_filter('block_editor_content', [ $this, 'filter_block_editor_content' ], 20, 2);
        // 处理REST API返回的内容(区块编辑器依赖此获取初始内容)
        add_filter('rest_prepare_post', [ $this, 'filter_rest_post_content' ], 10, 3);

        // 处理经典编辑器内容
        add_action('load-post.php', [ $this, 'load_classic_editor_content' ]);
        add_action('load-post-new.php', [ $this, 'load_classic_editor_content' ]);

    }

    /**
     * Save post_content to custom meta field when saving a draft or publishing a post/page.
     * @param $post_id
     * @param $post
     * @return void
     */
    public function save_post_content($post_id, $post): void
    {

        // Check if the post type is allowed.
        if (in_array($post->post_type, self::$allowed_post_types)) {

            // Set the post_content to the custom meta field.
            $post_content = $post->post_content;
            update_post_meta($post_id, 'post_content', $post_content);
            $post->post_content = ''; // Empty the default post_content field.

            // Unhook this function so it doesn't loop infinitely.
            remove_action('save_post', [ $this, 'save_post_content' ]);

            // Update the post, which calls save_post again.
            wp_update_post($post);

            // Re-hook this function.
            add_action('save_post', [ $this, 'save_post_content' ]);

        }
    }

    /**
     * Filter frontend content to load from custom meta.
     * @param $content
     * @return mixed
     */
    public function filter_the_content($content): mixed
    {

        // Get the current post.
        global $post;

        // Check if the post type is allowed.
        if ($post && in_array($post->post_type, self::$allowed_post_types)) {

            // Get the post_content from the custom meta field.
            $custom_post_content = get_post_meta($post->ID, 'post_content', true);
            if (!empty($custom_post_content)) {
                $content = $custom_post_content;
            }
        }

        // Return the content.
        return $content;
    }

    /**
     * Filter block editor content to load from custom meta.
     * @param string $content
     * @param WP_Post $post
     * @return string
     */
    public function filter_block_editor_content(string $content, WP_Post $post): string
    {
        if (in_array($post->post_type, self::$allowed_post_types)) {
            $custom_content = get_post_meta($post->ID, 'post_content', true);
            if (!empty($custom_content)) {
                return $custom_content;
            }
        }
        return $content;
    }

    /**
     * Filter REST API post response to return custom meta content.
     * @param WP_REST_Response $response
     * @param WP_Post $post
     * @param WP_REST_Request $request
     * @return WP_REST_Response
     */
    public function filter_rest_post_content(WP_REST_Response $response, WP_Post $post, WP_REST_Request $request): WP_REST_Response
    {
        if (in_array($post->post_type, self::$allowed_post_types)) {
            $custom_content = get_post_meta($post->ID, 'post_content', true);
            if (!empty($custom_content)) {
                $response->data['content']['raw'] = $custom_content;
            }
        }
        return $response;
    }

    /**
     * Load custom content into classic editor.
     * @return void
     */
    public function load_classic_editor_content(): void
    {
        global $post;
        if (!is_object($post) || !in_array($post->post_type, self::$allowed_post_types)) {
            return;
        }
        $custom_content = get_post_meta($post->ID, 'post_content', true);
        if (!empty($custom_content)) {
            $post->post_content = $custom_content;
        }
    }

}

new WP_JSON_Search();

关键修改说明

  1. 区块编辑器支持:

    • 保留block_editor_content钩子并提高优先级(20),确保覆盖默认内容
    • 添加rest_prepare_post钩子:区块编辑器初始化时会通过REST API获取文章内容,必须修改返回的content.raw字段,否则编辑器会加载空内容
  2. 经典编辑器支持:

    • 使用load-post.php和load-post-new.php钩子,在编辑页面加载时直接修改全局$post对象的post_content属性,确保经典编辑器加载自定义字段内容
  3. 原有功能保留:

    • 保留了原有的save_post和the_content钩子,确保内容迁移和前端展示正常工作

内容的提问来源于stack exchange,提问作者joshmoto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 01:10:55