Skip to content

Instantly share code, notes, and snippets.

@mgratch
Last active October 19, 2016 17:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgratch/8958f6a04c5c09a53a098d5aebd2965c to your computer and use it in GitHub Desktop.
Save mgratch/8958f6a04c5c09a53a098d5aebd2965c to your computer and use it in GitHub Desktop.
Batch Strip HTML tags from post/page content. BACKUP YOUR DB BEFORE USING THIS! The plugin should deactivate itself if it's successful.
<?php
/*
* Plugin Name: Remove HTML
* Description: batch Remove HTML from post_content
* Author: Marc Gratch
* Author URI: http://marcgratch.com
* Version: 0.1.0
*/
function rh_get_all_ids(){
$args = array ('post_type' => array('post','page'), 'fields' => 'ids', 'posts_per_page' => -1);
$all_posts = new WP_Query($args);
return $all_posts->posts;
}
function rh_strip_html_tags(){
$all_ids = rh_get_all_ids();
foreach($all_ids as $post_id){
$content = get_post_field( 'post_content', $post_id, 'raw' );
$content = strip_tags( $content, 'a,strong,p' ); //the second parameter are allowed tags
$post = array(
'ID' => $post_id,
'post_content' => $content,
);
wp_update_post( $post );
}
deactivate_plugins( plugin_basename( __FILE__ ) );
}
add_action( 'shutdown', 'rh_strip_html_tags' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment