The hook that you’re currently using is publish_<post type>
which means the <post type>
part is dynamic and the value is a post type name/slug.
So for other post types like page
and (a custom post type named) my_cpt
, you would just need to change the “post” (or the <post type>
part) in the hook name to page
, my_cpt
or whatever is the post type name/slug like so:
add_action( 'publish_post', 'notifyauthor' ); // for the default 'post' post type
add_action( 'publish_page', 'notifyauthor' ); // for the default 'page' post type
add_action( 'publish_my_cpt', 'notifyauthor' ); // for a CPT with the name/slug my_cpt
Or if you want your action (notifyauthor()
) to be called when any posts (regular Posts and Pages, and custom post types) is published, then you would want to use the transition_post_status()
hook instead of the publish_<post type>
hook.
Here’s an example based on this on the WordPress Developer Resources site:
function wpdocs_run_on_publish_only( $new_status, $old_status, $post ) {
// Yes, I said "any posts", but you might better off specify a list of post
// types instead. Or you could do the check in your notifyauthor() function
// instead.
$post_types = array( 'post', 'page', 'my_cpt', 'foo_bar', 'etc' );
if ( ( 'publish' === $new_status && 'publish' !== $old_status ) &&
in_array( $post->post_type, $post_types )
) {
notifyauthor( $post->ID );
}
}
add_action( 'transition_post_status', 'wpdocs_run_on_publish_only', 10, 3 );
PS: If you use the above code/hook, then you should remove the add_action('publish_post', 'notifyauthor');
from your current code.