Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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";
    }
}
share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

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().

share|improve this answer
    
Thanks. Can you give an opinion on which would be more approprite to hook to in order to do run a find/replace filter on the content prior to saving it? The filter is simply adding <b> tags around the "custom keyword phrase" assigned to the post by the end user. –  Scott B Nov 4 '10 at 14:49
    
also do you know if content_save_pre runs before balanceTags()? I'd like any replacements I do to be filtered for xhtml well formedness and cleaned up if necessary. –  Scott B Nov 4 '10 at 14:50
    
@Scott B: I always prefer the most specific hook for the task. balanceTags() runs in the content_save_pre hook, but with a priority of 50. So if you want to run before that, the default priority of 10 should be good. Since content_save_pre gets called before wp_insert_post_data (via sanitize_post() at the top of wp_insert_post()), balanceTags() will not be called on anything you do in wp_insert_post_data. –  Jan Fabry Nov 4 '10 at 15:06
    
Thanks Jan, I believe this is the right filter. Having problems with getting my filter function to run inside it, but that's another question. wordpress.stackexchange.com/questions/3638/… –  Scott B Nov 4 '10 at 15:19
add comment

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.

share|improve this answer
    
Thanks! Can I add update_custom_meta() operations there too? –  Scott B Nov 4 '10 at 14:26
    
I would continue updating custom meta on the 'save_post' filter. The 'wp_insert_post_data' filter both updates and inserts posts, so some may not have and ID yet. –  prettyboymp Nov 4 '10 at 14:29
    
@prettyboymp: Apparently the add_action for save_post passes $post differently than does the add_action for wp_insert_post_data. For example, when I echo $post in the latter, it comes across as an array instead of the actual post object that I get from save_post –  Scott B Nov 4 '10 at 14:32
    
Do I need to test for 'doing_autosave' in the wp_insert_post_data action? –  Scott B Nov 4 '10 at 14:35
    
Correct, the 'wp_insert_post_data' gets passed the raw post insert data as an array. If you set the parameter count to 2 for the add_filter call, you'll also get an array of the original posted data, which will allow you to check what the user originally submitted versus what is currently in the data that will be inserted. –  prettyboymp Nov 4 '10 at 19:39
show 1 more comment

Your Answer

 
discard

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.