Last active
February 5, 2020 16:52
-
-
Save kamalahmed/3b4ecc906965d093975028facf51c6a8 to your computer and use it in GitHub Desktop.
Detect when a new WordPress post is created or published. You can do some work ONLY when a NEW post/page/custom is created using the following gist.
This file contains hidden or 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 | |
/** | |
* @param string $new_status the new status of the post eg. publish | |
* @param string $old_status the old status of the post eg. draft, publish etc | |
* @param WP_Post $post | |
*/ | |
function prefix_do_something_when_a_new_post_published($new_status, $old_status, $post) { | |
// Do something On first publish | |
if ($new_status == 'publish' && $old_status != 'publish' && isset($post->post_type)) { | |
switch ($post->post_type) { | |
case 'post' : | |
// do something here when a new post is published for the first time | |
break; | |
case 'page' : | |
// do something here when a new page is published for the first time | |
break; | |
case 'your-custom-post' : | |
// do something here when a new 'your-custom-post' is published for the first time | |
break; | |
default: | |
// Silence is golden | |
break; | |
} | |
} | |
} | |
add_action('transition_post_status','prefix_do_something_when_a_new_post_published',10,3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment