WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)

share|improve this question
up vote 10 down vote accepted

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;
share|improve this answer
got me going in the right direction, thanks. i think you actually need to use add_action though instead of add_filter. also your wpse35931_filter_post_data and filter_handler are incongruent... – cwd Dec 9 '11 at 14:38
It is definitely a filter. Though filters and actions are based on the same functionality which allows some crossover. If you use this as an action, however, you will not be able to return the data which would remove the whole point. You want to FILTER the data and then return it. – Jake May 13 at 15:52

Use filter content_save_pre exactly like the_content -- the difference is that it operates when a post is saved, rather than displayed.


share|improve this answer

If you just want to add something similar at the end of all the posts, then I would suggest you use the the_content filter.

function append_to_content( $content ) {
    global $post;
    return $content.'<br />This post was saved on '.$post->post_date;
add_filter( 'the_content', 'append_to_content' );
share|improve this answer
Thanks but I actually want to edit the data before it is saved in the database. – cwd Dec 9 '11 at 14:25

add the following code to active theme.

Here I am replacing with [shell] before saving.

add_filter( 'content_save_pre' , 'my_sanitize_content' , 10, 1);
function my_sanitize_content( $value ) {
          return str_replace("<shell>", "[shell]", $value );
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.