Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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