So I found a solution. There are 4 hooks that can be used to accomplish this depending on the exact needs.
The hooks are from wp-includes/meta.php
in functions update_metadata()
and add_metadata()
.
Hooks:
update_postmeta
updated_postmeta
add_post_meta
added_post_meta
These are called at different states and from the names it is pretty self self explanatory. add_post_meta
and update_postmeta
are called right before any DB changes and updated_postmeta
and added_post_meta
are called right after any changes to the DB.
Example:
//Example usage for updated and added.
function page_template_check( $meta_id, $post_id, $meta_key, $meta_value ) {
// Stop if not the correct meta key
if ( $meta_key != '_wp_page_template' ) {
return false;
}
//Do stuff here
};
add_action( 'added_post_meta', 'page_template_check', 10, 4 ); //after add
add_action( 'updated_postmeta', 'page_template_check', 10, 4 ); //after update