1

I have to send a Firebase Cloud Message (FCM) to an android application when a new post is inserted in a wordpress blog, I already put my code in the wp_includes\post.php , but this will not work, cause of the future WP update(post.php will be replaced and I can't insert my code evry time), so I have to put my code(it works no problem in this) in another place.. where I can put my file?? I have to write a plugin? It's possible to write a plugin that override a function? in this case wp_insert_post? And if this is possible where i retrieve the post data to send via FCM? Can you make an example or link a good example of code if this is possible?

Now my code is inside the wp_insert_post function and so it's executed when you push the button "publish" in new post area.

  • 3
    You would want to create a plug-in and then probably hook it in to the function in core, not override it. – Cedon Mar 21 '17 at 0:45
3

If you look through the definition of wp_insert_post(), you'll see

do_action( 'wp_insert_post', $post_ID, $post, $update );

at the end of it. So you can hook into this and perform whatever task you want to after creating a post.

Something like this in your theme's functions.php or in your plugin file:

do_action( 'wp_insert_post', 'send_notification');
function send_notification($post_ID, $post, $update){
    //your code here
}
| improve this answer | |
  • 1
    This looks like a suitable solution, but I wouldn't put this into the theme's functions.php file... a change in the active theme and you will lose the functionality. Plus this functionality has nothing to do with the design of the website so it should not go into your theme files. Rather create a plugin, it's really easy to do, you just need a php file with the proper header and then put that file into your wp-content/plugins folder. Try to look at the hello dolly plugin for the required header. – bynicolas Mar 22 '17 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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