rvanweerd
12/1/2014 - 1:06 PM

Strip tags, replace, and more in WordPress database

Strip tags, replace, and more in WordPress database

<?php
/*
 * Plugin Name: rvw Strip Tags
 * Version: 0.0.2
 * Description: Strip html tags from the Content, leave links and images
 * Plugin URI: http://weerdpress.com
 * Author: Ronald van Weerd
 * Author URI: http://weerdpress.com
 * Changelog: see history.txt
*/

if (!class_exists('rvw_strip_tags')) {

    class rvw_strip_tags {
        
        // the array with the replacement string(s)
        var $replacements = array(
            'old string' => 'new string'
        );

        function rvw_strip_tags() {

            if (is_admin()) {
                $this->load_admin();
            }
                    
        }
                
        function load_admin() {

            add_action('admin_menu', array($this, 'rvw_add_strip_tags_page') );
                    
        }
        
        function rvw_add_strip_tags_page() {

            if ( function_exists('add_management_page') ) {

                add_management_page('RVW Strip Tags', 'RVW Strip Tags', 10, basename(__FILE__), array($this, 'rvw_strip_tags_page'));

            }

        }

        function rvw_strip_tags_page() {

            if (isset($_POST['start'])) { // final run

                $applychanges = true;
                $this->rvw_strip_tags_process($applychanges);

            } else if (isset($_POST['test'])) { // test run

                $applychanges = false;
                $this->rvw_strip_tags_process($applychanges);

        } ?>

        <div class=wrap>
            <form method="post">
                <h2>Strip Tags</h2>
                <p>This plugin will go through each post looking certain tags or content, and remove or replace them.<br />
                    The rules are <strong>customised for each particular site</strong>, so the plugin should not be distributed and used on other sites "as is", as results may be unpredictable.</p>
                <p>You can change the code in the plugin, or you can <a href="mailto:ronald@vanweerd.com">contact me</a> to customise the remove/replacement rules for your specific site.</p>

                <p>Note: This plugin <strong>will</strong> update your WordPress posts table. <strong>ALWAYS</strong> make a backup before using this plugin.</p>

                <div class="submit">
                    <input type="submit" name="start" value="Final Run" />
                    <input type="submit" name="test" value="Test Run" />
                </div>

            </form>
        </div>
        <?php 
        }

        function rvw_strip_tags_process($applychanges = true) {

            global $wpdb;

            $results = $wpdb->get_results("SELECT ID, post_content, post_title FROM {$wpdb->posts} where post_type = 'post'");

            $total = count($results); 
            $changed = 0;

            echo '<div class="updated"><p>';

            foreach( $results as $entry ){

                $new_title = str_replace(array_keys($this->replacements), $this->replacements, $entry->post_title);

                if ( $entry->post_title !== $new_title ) {

                    if ( $applychanges ) {
                        $wpdb->query( $wpdb->prepare(
                            "UPDATE {$wpdb->posts} SET post_title = %s WHERE ID = %s", $new_title, $entry->ID) );
                            echo "Updated <em>" . $entry->post_title . "</em> to <em>" . $new_title . "</em><br />";
                    } else {
                            echo "Post: <em>" . $entry->post_title . "</em> will be updated to <em>" . $new_title . "</em><br />";
                    }

                    $changed++;
                }

            }

            if ( $applychanges ) {
                printf("FINAL: %d out of %d posts have been updated", $changed, $total);
            } else {
                printf("TEST RUN: %d out of %d posts will be updated", $changed, $total);
            }

            echo '</p></div>';

        }
    }

}

// Instantiate the class
if (class_exists('rvw_strip_tags')) {
        $rvw_strip_tags = new rvw_strip_tags();
}