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

An external service that uses XML RPC to publish some posts on my site adds some unwanted CSS styles and HTML to the post content.

I would like to clean it up using a HTML cleaner that I've built based on DOMDocument and DOMXPath.

My only question is how can I access the post_content before it is actually saved to the databse so I can run my cleaner on it? Perhaps there is a filter/action that I can use?

share|improve this question
I'd look into sanitize_post_field. This is a comment, not an answer, because I am not sure. – s_ha_dum Dec 12 '12 at 15:13

From the save_post Codex page:

save_post is an action triggered whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email.

So, if you hook into save_post you can run your filter before save:

add_action( 'save_post', 'wpse_75871_save_post' );

function wpse_75871_save_post( $post_id ) {

    if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
        // Avoid infinite loops
        remove_action( 'save_post', 'wpse_75871_save_post' );

        $post = get_post( $post_id );

        $your_filtered_content = get_your_filtered_content( $post->post_content );

                'ID' => $post_id, 
                'post_content' => $your_filtered_content 

        add_action( 'save_post', 'wpse_75871_save_post' );

You may need to do some other checking for your specific case. Let me know if you have any questions!

share|improve this answer
Is there a way to check if this save_post was triggered by XML-RPC? I would rather use it only for XML-RPC requests, not for all save_post actions, if possible. – ragulka Dec 13 '12 at 11:15
Indeed, it appears a constant XMLRPC_REQUEST is defined - I've updated the code to check against this. I haven't tested, so let me know if that does it! – Andy Adams Dec 13 '12 at 18:09

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.