28

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:

save_post
wp_insert_post

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:

add_action('whatever_hook_name','my_function');

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.

Update

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)

1
  • Note: You have a bad design :-) 1. everytime post would be saved, this string would be appended. (if you don't delete the previous one, you get a lot's of "This post was..."s 2. data such this one should be stored as a meta value of the post :-) BTW: save_post is called after data was saved, therefore after it was saved to database (not what you want).
    – jave.web
    Jan 20, 2017 at 17:56

5 Answers 5

43

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;
}
6
  • 2
    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, 2011 at 14:38
  • 2
    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, 2016 at 15:52
  • 4
    wp_insert_post_data works, but is a bit overpowered for what OP (and perhaps others) want to accomplish. A simpler option in this situation is content_save_pre as recommended by @drzaus.
    – rinogo
    Nov 17, 2016 at 16:05
  • 1
    Developer docs for wp_insert_post_data filter: developer.wordpress.org/reference/hooks/wp_insert_post_data
    – jave.web
    Jan 20, 2017 at 17:58
  • Does this hook get called whenever you a) manually call wp_insert_post() in, for example, functions.php of your theme to insert/update a post OR b) whenever a post is saved by any means possible (e.g. thru backend, thru frontend, somehow else...)?
    – Vadim H
    Jul 25, 2018 at 8:27
10

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

http://codex.wordpress.org/Plugin_API/Filter_Reference/content_save_pre

5

You can also check for the hook pre_post_update

add_action('pre_post_update', 'before_data_is_saved_function');

function before_data_is_saved_function($post_id) {

}
2
2

Add the following code to the active theme to replace <shell> 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);
 }
-1

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' );
1
  • Thanks but I actually want to edit the data before it is saved in the database.
    – cwd
    Dec 9, 2011 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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