How can I edit post data before it is saved?

I have a plugin and I would like to be able to run the post content through some filters before it is saved to the database. From looking at the plugin api, I see that two hooks that look like they might be helpful:


The only problem is that it looks like save_post does not need return a variable, and so I don’t know how to filter the content, and wp_insert_post looks documented.

I’d like to do something like this:


function my_function($post_content){
    return $post_content.' <br> This post was saved on '.time();

I am going to do something more useful than append a timestamp, namely running some regex filters, but this is the general type of filter / action I’m trying to add.


Please note that I want to intercept the data on it’s way to being saved in the database – not when it is being displayed in the post (eg: Not by adding a filter to the_content)


The wp_insert_post_data filter can do that:

add_filter( 'wp_insert_post_data' , 'filter_post_data' , '99', 2 );

function filter_post_data( $data , $postarr ) {
    // Change post title
    $data['post_title'] .= '_suffix';
    return $data;

Leave a Comment