Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**********************************************
- * Search & Replace for Marvin Shaker *
- * Place this script in your WordPress root *
- * and load in your web browser. Then wait. *
- **********************************************/
- /***** CUSTOMIZE HERE MAYBE **/
- // Batch size: how many post to fix in one pass
- // If your server is slow, reduce the number
- $marv_chunk = 50;
- /***** PROBABLY DONT EDIT THIS **/
- // search pattern
- $marv_search = '/\[tab:(.+)?\]/';
- // replacement pattern
- $marv_replace = '<!--nextpage--><!--marvin:\\1-->';
- // Don't replace first occurence found in post, remove only
- $marv_remove_first = true;
- /***** DON'T EDIT ANYTHING PAST THIS LINE **/
- // Load the WordPress Environment
- require('./wp-load.php');
- echo "<pre>\n";
- // start offset
- $marv_start = ( isset( $_GET['start'] ) ? $_GET['start'] : 1 );
- // total updated
- $marv_total = ( isset( $_GET['total'] ) ? $_GET['total'] : 0 );
- if( $marv_total )
- echo "So far, $marv_total posts updated...\n\n";
- // Setup post query
- query_posts( array(
- 'posts_per_page' => $marv_chunk,
- 'paged' => $marv_start
- )
- );
- // Loop through posts if any
- if ( have_posts() ) {
- while ( have_posts() ) : the_post();
- $post = marv_trim( get_the_content() ); // post content
- $new_post = marv_search_and_replace( $post ); // post content, fixed
- // Post needs to be updated
- if( $post != $new_post ) {
- // Set new post content
- $updated_post = array(
- 'ID' => $id,
- 'post_content' => $new_post,
- );
- // Update post
- if( wp_update_post( $updated_post ) ) {
- echo "Post #$id updated\n";
- // Update counter
- $marv_total++;
- // log if there was an error
- } else {
- error_log( "Error: could not update post $id\n", 3, dirname( __FILE__ ).'/marv-error.log' );
- }
- // Post doesn't need to be updated
- } else {
- echo "Post #$id skipped\n";
- }
- endwhile;
- // Load next batch of posts
- marv_next_page( );
- } else {
- echo "All done !! $marv_total posts were updated.";
- }
- function marv_trim( $string ) {
- return trim( $string, "\r\n\t " );
- }
- function marv_search_and_replace( $content ) {
- global $marv_search, $marv_replace, $marv_remove_first;
- if( $marv_remove_first )
- $content = preg_replace( $marv_search, '' , $content, 1 );
- $content = preg_replace( $marv_search, $marv_replace , $content );
- return marv_trim( $content );
- }
- function marv_next_page( ) {
- global $marv_start, $marv_chunk, $marv_total;
- $marv_start += $marv_chunk;
- $url = basename( __FILE__ )."?start=$marv_start&total=$marv_total";
- echo <<<SCRIPT
- <a href="$url">Next page</a> loading, please wait...
- <script type="text/javascript">
- function letsgo() {
- window.location="$url";
- }
- setTimeout( letsgo, 1500 );
- </script>
- SCRIPT;
- }
Add Comment
Please, Sign In to add comment