Frage

I'm trying to setup a function in functions.php that removes an image from the post_content on save. Everything works up until the time I'm trying to save the content, the page just hangs and I get a '500 Internal Server' error.

function remove_post_image( $post_ID ){

  // get the post
  $the_post = get_post($post_ID);

  // get the content of the post
  $post_content = $the_post->post_content;

  // replace any images
  $content = preg_replace("/<img[^>]+\>/i", "", $post_content);

  // save the post
  $my_post = array();
  $my_post['ID'] = $post_ID;
  $my_post['post_content'] = $content;

  // Update the post into the database
  wp_update_post( $my_post );   

  return $post_ID;
}
add_action('save_post', 'remove_post_image');

I fear I'm missing something very basic. Can anyone spot something I'm doing wrong?


edit

Already I see how silly this was setup. I'm having success with the following, but still would like to know if this is the best way? I'm using content_save_pre action...

function remove_post_image( $content ){

  // replace any images
  $content = preg_replace("/<img[^>]+\>/i", "", $content);

  return $content;
}
add_action('content_save_pre', 'remove_post_image');

Keine korrekte Lösung

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit wordpress.stackexchange
scroll top