Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active August 29, 2015 14:00
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tommcfarlin/55eea73a6a1b3e8d22ca to your computer and use it in GitHub Desktop.
[WordPress] An example of how to avoid getting stuck in an infinite loop of events when using `save_post`.
<?php
class AcmeSavePost {
public function __construct() {
add_action( 'save_post', array( $this, 'modify_post_content' ) );
}
public function modify_post_content( $post_id ) {
$post = array(
'ID' => $post_id,
'content' => ''
);
remove_action( 'save_post', array( $this, 'modify_post_content' ) );
wp_update_post( $post );
add_action( 'save_post', array( $this, 'modify_post_content' ) );
}
}
<?php
class AcmeSavePost {
public function __construct() {
add_action( 'save_post', array( $this, 'modify_post_content' ) );
}
public function modify_post_content( $post_id ) {
$post = array(
'ID' => $post_id,
'content' => ''
);
wp_update_post( $post );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment