在 WordPress 经典和块编辑器中过滤内容以从自定义字段加载内容

Filter content in WordPress classic and block editor to load content from custom field

提问人:joshmoto 提问时间:11/16/2023 更新时间:11/17/2023 访问量:51

问:

这个让我难住了。

我正在将数据卸载到名为 的自定义字段。请参阅问题末尾的课程,看看我是怎么做到的。wp_posts > post_contentpost_content

我正在尝试过滤加载到我的经典编辑器或块编辑器中的内容,因此内容实际上是从我的自定义字段加载的,称为 ,而不是从默认表加载。post_contentwp_posts

我已经尝试了这 3 个过滤器和操作(不是同时进行的,也没有运气)......

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

这些都没有成功从 WordPress 编辑器中的自定义字段加载内容,即使字段中存在带有块编辑器内容代码的“post_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();

如果有人对我如何在编辑器中过滤内容以从自定义字段加载数据有任何建议,那将非常有用。

WordPress 过滤器 编辑器 操作

评论


答:

-1赞 Asel nawagamuwa 11/16/2023 #1
class WP_JSON_Search {

    // ... (existing code)

    public function __construct () {

        // Use 'edit_form_after_title' action to load custom post content into the editor.
        add_action('edit_form_after_title', [ $this, 'load_custom_post_content_editor' ]);

        // Use 'save_post' action to save post_content to the custom meta field.
        add_action('save_post', [ $this, 'save_post_content' ], 10, 2);

        // Use 'the_content' filter to filter the content in the editor.
        add_filter('the_content', [ $this, 'filter_the_content' ]);

    }

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

    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.
        }
    }

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

所做的更改:

用于将自定义帖子内容加载到编辑器中的操作。此操作在显示编辑器之前发生。 从方法中删除了 和调用。它们会导致不必要的递归,并可能干扰预期的行为。edit_form_after_titleremove_actionwp_update_postsave_post_content

评论

0赞 joshmoto 11/16/2023
感谢您的回答!不幸的是,这不起作用。数据被卸载到自定义字段正常,但通过删除内容,表中的内容不会被清空......https://imgur.com/a/H66nqVo。 也不加载自定义字段,编辑器内容仍在从表中加载。wp_update_postsave_post_contentwp_postsload_custom_post_content_editorpost_contentwp_posts
1赞 Chris 11/17/2023 #2

Gutenburg 的编辑器使用 WordPress 的内部 REST API。您可以利用它并操纵发回的响应。

添加以下筛选器:

// filter our rest post content
add_filter('rest_post_content', function (array $content, string $type, int $post_id) {

    $content['raw'] = 'Updated content!';

    return $content;
}, 10, 3);

// filter the rest response
add_filter('rest_post_dispatch', function (WP_Rest_Response $object) {
    if (
        !property_exists($object, 'data')
        || !array_key_exists('content', $object->data)
        || !array_key_exists('type', $object->data)
        || !array_key_exists('id', $object->data)
    ) {
        return $object;
    }

    $object->data['content'] = apply_filters(
        'rest_post_content',
        $object->data['content'],
        $object->data['type'],
        $object->data['id']
    );

    return $object;
}, 10, 1);

rest_post_dispatch是一个内部 WordPress 过滤器,您可以检查响应,如果所有数据都在那里,请在数据发送到 Gutenburg 编辑器之前应用自定义过滤器来操作您的数据!rest_post_content

评论

1赞 joshmoto 11/17/2023
哦,是的,这效果很好。非常聪明地利用了WP内部api,这是块编辑器从中获取数据的地方。比我在那里看到的其他 js hacks 好得多。👍🏼