dragunoff

wpquestions.com, ID = 2838

Aug 14th, 2011
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.02 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Template name: Update posts regex
  4.  */
  5.  
  6. // query for posts
  7. query_posts( array(
  8.     'post_type' = 'any', // get all post types
  9.     'posts_per_page' = -1, // all posts
  10.     'nopaging' = true // no pagination
  11.     )
  12. );
  13.  
  14. // loop
  15. if ( have_posts() ) : while ( have_posts() ) : the_post();
  16.  
  17.     // set vars
  18.     $first_match_pattern = '/\[tab:.+?\]\r/g'; // regex pattern to match
  19.     $pattern = '/\[tab:.+?\]/g'; // regex pattern to match
  20.     $replacement = '<!-- nextpage -->'; // replacement string
  21.     $string = get_the_content(); // post content
  22.  
  23.     // remove the first match (along with the linebreak)
  24.     $updated_content = preg_replace( $first_match_pattern,'' , $string, 1 );
  25.     $string = $updated_content;
  26.     // match and replace the rest
  27.     $updated_content = preg_replace( $pattern, $replacement, $string );
  28.  
  29.     // Set new post content
  30.     $my_post = array();
  31.     $my_post['ID'] = get_the_ID();
  32.     $my_post['post_content'] = $updated_content;
  33.  
  34.     // Update the post into the database
  35.     $result = wp_update_post( $my_post );
  36.  
  37. endwhile; endif;
  38.  
  39. ?>
Advertisement
Add Comment
Please, Sign In to add comment