0

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?

0

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 );

        wp_update_post(
            array(
                '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!

  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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