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

问题描述 投票:0回答:1

这个把我难住了。

我正在将

wp_posts > post_content
数据卸载到名为
post_content
的自定义字段。请参阅问题末尾的课程,了解我是如何做到这一点的。

我正在尝试过滤加载到经典编辑器或块编辑器中的内容,因此内容实际上是从名为

post_content
的自定义字段加载的,而不是从默认的
wp_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 filter editor action
1个回答
0
投票
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();

所做的更改:

Used edit_form_after_title action to load custom post content into the editor. This action occurs before the editor is displayed.
Removed the remove_action and wp_update_post calls from the save_post_content method. They were causing unnecessary recursion and might interfere with the expected behavior.
© www.soinside.com 2019 - 2024. All rights reserved.