I want to send my post to Telegram when it is created. I have read discussion here and inspected these code from n7studio, but my function send_posts_to_Telegram doesn't fire when I publish post. Isn't rest_after_insert_post fire when post are being published from REST API and Gutenberg?

add_action('rest_after_insert_post', 'send_posts_to_Telegram', 10, 3);

function send_posts_to_Telegram($post, $request, $creating = true){
//Send post to Telegram
  • "my code doesn't work" - how does it not work? Your function isn't getting called? And are you referring to the default post post type? If not, then you should use rest_after_insert_<post type>.
    – Sally CJ
    Commented Mar 8, 2020 at 15:26
  • The function wasn't called, and yes I am referring to the default post type. Any idea why? Btw I will edit my question to make it more specific. Thanks!
    – Winston
    Commented Mar 8, 2020 at 16:55
  • Are you very sure the function isn't getting called? Have you tried something simple like function send_posts_to_Telegram() { error_log( 'send_posts_to_Telegram called' ); } and check the PHP's error_log/logs file? And where did you put your code? In the theme functions.php file? In a plugin? But your code seems fine and yes, WordPress fires the rest_after_insert_post hook when a post is edited/created via the block editor.
    – Sally CJ
    Commented Mar 9, 2020 at 18:36
  • 2
    So you are right! The problem was in my own function but not with the rest_after_insert_post. It did fire as it should. Didn't realize there is something call error_log() and now I see how useful it is. Thanks!
    – Winston
    Commented Mar 10, 2020 at 8:54


Your Answer

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

Browse other questions tagged or ask your own question.