How to filter $post->post_content prior to save
-
16-10-2019 - |
Question
I have a function inside my plugin that attaches to save_post, and inside that function, I'm seeking to filter the post_content prior to it being saved. However, the content never actually gets changed once its saved. See below where I'm setting $post->post_content = "test";
add_action('save_post', 'save_post_filter',10,2);
function save_post_filter($postID, $post){
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $postID;
}
else
{
if($parent_id = wp_is_post_revision($postID))
{
$postID = $parent_id;
$post = get_post($postID);
}
$post->post_content = "test";
}
}
Solution
The content_save_pre
filter is applied to the content before it gets saved in the database. Some default filters hook into it too, for example balanceTags()
and wp_filter_post_kses()
.
OTHER TIPS
The 'save_post' filter runs after the post is filtered. If you need to modify the content before it is saved, try the 'wp_insert_post_data' filter.
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange