Last active
June 20, 2018 23:13
-
-
Save ammist/ffae406e16b2663d7b28235863d19d48 to your computer and use it in GitHub Desktop.
WordPress Save Hook Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// called when saving a post. | |
public function tl_save_post($post_id){ | |
// dont do this on auosave. | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | |
return; | |
// verify this came from the our screen and with proper authorization, | |
// because save_post can be triggered at other times | |
$post = get_post($post_id); | |
if (!isset($post->post_type) || 'my_post_type' != $post->post_type ) { | |
return; | |
} | |
// unhook this function so it doesn't loop infinitely | |
remove_action( 'save_post', 'tl_save_post, 100 ); | |
// do stuff that might trigger another Save | |
/* stuff here */ | |
// put back the hook | |
add_action( 'save_post', 'tl_save_post, 100 ); | |
} | |
add_action( 'save_post', 'tl_save_post, 100 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment