How to modify post content before writing to database?

You generally use add_filter before displaying information. For your case you can use add_action('publish_post', 'your_function') and then you intercept $_POST values. The content of the WYSIWYG editor is available through $_POST['content'].

The hook is of type publish_{post_type} btw. You should change accordingly.

Example:

function wpse_89292_save_my_post()
{
print_r( $_POST['content'] ); # will display contents of the default wysiwyg editor
die;
}
add_action('publish_post', 'wpse_89292_save_my_post');

Using wp_insert_post_data filter:

function wpse_89292_save_my_post( $content ) {
  global $post;
  if( isset($post) && get_post_type( $post->ID ) == 'post' ){
    $content['post_content'] = function_to_manipulate_the_content();
  }
  return $content;
}
add_filter( 'wp_insert_post_data', 'wpse_89292_save_my_posts' );

Leave a Comment