0

In my blog I have set up auto-publish for posts and I get 5 thousands new posts every day, in this moment I have more than 150 thousand posts.

I need a script, or a plugin, or a function to connect to my database and strip out all img tags from post content.

I know the regular expression to match the img tags, but I can't find a script for using this regular expression

<img[^>]+\>  

to replace imgs in already published posts and to prevent new posts to contain image tags.

I found this class and I think that it will achieve my goal but I am not sure how to edit this

$wpdb->query(
  "UPDATE $wpdb->posts SET post_parent = 7
  WHERE ID = 15
  AND post_status = 'static'"
);

to fit my needs.

please note: my site is using some auto publish posts feature Any ideas!

closed as off-topic by chrisguitarguy, tfrommen, kaiser, s_ha_dum, brasofilo Oct 1 '13 at 21:48

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?" – chrisguitarguy, tfrommen, kaiser, s_ha_dum, brasofilo
If this question can be reworded to fit the rules in the help center, please edit the question.

1

You can use the function get_posts to get all your post, and then use a foreach cycle to replace the img tags using the regex you posted and then use wp_update_post to change post content:

function remove_images_form_past_posts() {    
  if ( get_transient('images_removed_from_past') ) return;
  $posts = get_posts('nopaging=1');    
  if ( $posts ) {
    foreach ( $posts as $post ) {
      $newcontent = preg_replace('/<img[^>]+\>/i', '', $post->post_content);
      $newpost = array( 'ID' => $post->ID, 'post_content' => $newcontent);
      wp_update_post($newpost);
    }
    set_transient('images_removed_from_past', 1);
  }
}

add_action('admin_init','remove_images_form_past_posts');

This code should be used only once, to change the past posts content. To ensure it runs only once I've used a transient. See Codex for Transient API.

Now you have to add a function that hook into 'wp_insert_post_data' filter hook and prevent new posts are added with images:

function remove_images_before_publish( $data, $postarr ) {
  if ( ! empty($data['post_content']) )
    $data['post_content'] = preg_replace('/<img[^>]+\>/i', '', $data['post_content']);
  return $data;
}
add_filter('wp_insert_post_data','remove_images_before_publish', 20, 2);
  • fine, but how to implement this with $wpdb . or you mean that i don't need $wpdb at all? – adnan Oct 1 '13 at 13:37
  • using this once! but i need to using this every day because i have 5 thousands new post every day. can i add to your code where expression to put post_date? – adnan Oct 1 '13 at 13:52
  • i will do, but it is true i need to update my past & future posts @G. M. – adnan Oct 1 '13 at 14:03
  • iam already updated it, thank you very very much for your help, iam really appreciate your help. iam sorry for this stupid question where i can put this code. or how to make this a plugin? – adnan Oct 1 '13 at 14:22
  • iam already voted & accepted your answer, does this 2 functions will use alot of memory or slow down the site load speed – adnan Oct 1 '13 at 14:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.