Last active
March 6, 2018 11:42
-
-
Save daggerhart/6249b4929eb1ca0cfe15 to your computer and use it in GitHub Desktop.
Wordpres: Clean up content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function __cleanup_post_data(){ | |
global $wpdb; | |
// default wordpress "allowed html" for posts | |
$allowed_tags = wp_kses_allowed_html( 'post' ); | |
// don't allow spans | |
unset($allowed_tags['span']); | |
// don't allow inline styles, nor classes | |
foreach($allowed_tags as $tag => &$atts ){ | |
if (isset($atts['style'])){ | |
unset($atts['style']); | |
} | |
if (isset($atts['class'])){ | |
unset($atts['class']); | |
} | |
} | |
// html comments pattern to search for | |
$html_comment_pattern = '/<!-- [\s\S] ?*-->/s'; | |
// 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 and remove comments | |
$post->post_content = wp_kses( $post->post_content, $allowed_tags ); | |
$post->post_content = preg_replace( $html_comment_pattern, '', $post->post_content ); | |
// clean excerpt and remove comments | |
$post->post_excerpt = wp_kses( $post->post_excerpt, $allowed_tags ); | |
$post->post_excerpt = preg_replace( $html_comment_pattern, '', $post->post_excerpt ); | |
// 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