I have following code in my custom plugin. Link is a custom post type.

function post_published_notification( $ID, $post ) {
add_action( 'publish_link', 'post_published_notification', 10, 2 );

This works fine when I publish / Update a link via WordPress dashboard.

But it is not working when I programitically use wp_publish_post( $post_id ).

Why is that? How to make it work?

1 Answer 1


wp_publish_post - Publish a post by transitioning the post status.

Note: This function does not do anything except transition the post status. If you want to ensure post_name is set, use wp_update_post() instead.

publish_{$custom_post_type} - publish_post is an action triggered whenever a post is published, or if it is edited and the status is changed to publish.

After running wp_publish_post a few events are triggered:

  • do_action( 'edit_post', $post->ID, $post );
  • do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
  • do_action( 'save_post', $post->ID, $post, true );
  • do_action( 'wp_insert_post', $post->ID, $post, true );
  • wp_transition_post_status( 'publish', $old_status, $post );
    • do_action( 'transition_post_status', $new_status, $old_status, $post );
    • do_action( "{$old_status}to{$new_status}", $post );
    • do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );

So in theory your 'publish_link' should work because it fits the pattern



500 Error

For the sideload issue, just create a function with some default params so you have all the args required to sideload.

function sideload_media_file($url = '', $post_id = NULL, $desc = NULL, $return = 'src') {
    require_once(ABSPATH . 'wp-admin/includes/media.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/image.php');

    return media_sideload_image( $url, $post_id, $desc, $return);

sideload_media_file($url, NULL, NULL, 'src');
  • Thanks for the details answer. Is there any place to find what events are trigger for each each function.
    – Sandun
    Nov 3, 2016 at 5:03
  • Just look through github.com/WordPress/WordPress. But obviously {$post->post_type} is going to depend on your custom post types.
    – jgraup
    Nov 3, 2016 at 5:05
  • wp_update_post() is also not working. Actually this is what happen. It gives 500 error when I use wp_update_post() or wp_publish_post. But it works exactly fine when I change status to "publish" and update using WordPress dashboard, Any idea what is the problem?
    – Sandun
    Nov 3, 2016 at 6:19
  • You should enable LOGGING codex.wordpress.org/WP_DEBUG and track down errors. 500 means something is up with your server. If you are triggering directly, you should also be able to call your function directly as well. You should also check the value of $post_id first to make sure it's a valid type. Also what is the value of $wpdb? Because maybe there is an issue there. See github.com/WordPress/WordPress/blob/master/wp-includes/… - To track down what in that function is the cause, then take out all those function calls & run them directly to see where it breaks.
    – jgraup
    Nov 3, 2016 at 12:21
  • Also check when you are calling this function? is it too late? Are you on the front-end or back-end? What page are you calling it from? More details should help here.
    – jgraup
    Nov 3, 2016 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.