在 WordPress 中修改 $post->post_content

Modifying $post->post_content in WordPress

我使用的模板似乎在搜索页面中输出 $post->post_content

我维护了一个使用非标准短代码格式的插件,我正在尝试找出 如何在 $post->post_content 显示之前对其进行过滤 因为目前我的简码没有被覆盖(同样,没有使用简码 API)。

这让我难住了。任何帮助,我将不胜感激。

我认为您可以使用 the_post 操作挂钩,它允许在查询和设置后立即修改 post 对象:

add_action('the_post', function($post, $query){

    // do whatever you want to $post, for example:
    $post->post_content = str_replace('{YOUR_SHORTCODE}', 'WHATEVER', $post->post_content);

}, 10, 2);