Skip to content

Instantly share code, notes, and snippets.

@daggerhart
Created July 27, 2015 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daggerhart/ef28ee65e75328fa5a07 to your computer and use it in GitHub Desktop.
Save daggerhart/ef28ee65e75328fa5a07 to your computer and use it in GitHub Desktop.
WordPress post data cleanup. Very basic.
<?php
/**
* Example of using wp_kses() to clean up WordPress post data.
*/
function __cleanup_post_data(){
global $wpdb;
// default wordpress "allowed html" for posts
$allowed_tags = wp_kses_allowed_html( 'post' );
// posts
$posts = $wpdb->get_results('SELECT ID,post_content,post_excerpt FROM wp_posts WHERE post_content like "%style%" OR post_excerpt like "%style%"');
foreach( $posts as $i => $post ){
// clean content
$post->post_content = wp_kses( $post->post_content, $allowed_tags );
// clean excerpt
$post->post_excerpt = wp_kses( $post->post_excerpt, $allowed_tags );
// update row in the db with new values
$update_sql = $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = %s, post_excerpt = %s WHERE ID = %d",
$post->post_content,
$post->post_excerpt,
$post->ID );
$wpdb->query( $update_sql );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment