1

i'm trying to send push notification when the wordpress admin insert new post.

I was looking for hours where to add my function and after a lot i found core function wp_insert_post inside wp-includes/post.php

that function returns the Post_ID, so i added my custom script before return:

include '../push/send_message.php';
sendNotification($postarr['post_title'],$postarr['guid']);

The problem is that when i import these two rows here

function wp_insert_post($postarr, $wp_error = false) {
    ...
    if ( $update ) {
        do_action('edit_post', $post_ID, $post);
        $post_after = get_post($post_ID);
        do_action( 'post_updated', $post_ID, $post_after, $post_before);
    }
    do_action('save_post', $post_ID, $post);
    do_action('wp_insert_post', $post_ID, $post);
    include '../push/send_message.php';
    sendNotification($postarr['post_title'],$postarr['guid']);
    return $post_ID;
}

nothing happen and when admin insert new post, the success page is blank. if i remove that line all is ok.. very strange.. Can someone give me a right way to do what i need?

3
  • What do you mean by "added my custom script before return"? Are you hacking the Core file?
    – s_ha_dum
    Commented Jul 8, 2013 at 16:26
  • if you see my answer, you can see that before return $post_ID i've added my two custom rows. I never worked with wordpress and i don't know how to do what i need and this is for me the smart solution.
    – Jayyrus
    Commented Jul 8, 2013 at 16:27
  • That is a core function, you should never alter the core files.
    – Wyck
    Commented Jul 8, 2013 at 16:36

2 Answers 2

3

See those two hooks in the hacked Core code you posted?

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

Those are what you need to use to do this right.

function run_on_update_post($post_ID, $post) {
    var_dump($post_ID, $post); // debug
    include '../push/send_message.php';
    sendNotification($post['post_title'],$post['guid']);
    return $post_ID;

}
add_action('save_post', 'run_on_update_post', 1, 2);

save_post runs every time the post is saved. I can't swear that your function will work but you should be able to modify things to make it work. Just look at the var_dump and alter accordingly. The includes may not work either, as they are relative paths. You may have to change that as well.

4
  • Where i need to add add_action?
    – Jayyrus
    Commented Jul 8, 2013 at 19:35
  • Putting that in a plugin would be best but it will work from the theme's functions.php.
    – s_ha_dum
    Commented Jul 8, 2013 at 19:37
  • ok, i add your code inside a plugin that i've activate. Inside callback function,i put a simple print_r($_POST);. The problem is that i see that only when i click on "create new article" and not when i publish it. How can i edit to achive what i need?
    – Jayyrus
    Commented Jul 8, 2013 at 20:29
  • 1
    Put die; after your print_r($_POST);. Do you see it now?
    – s_ha_dum
    Commented Jul 8, 2013 at 20:39
1

Modifying core files is a no-go. The Plugin API provides the connection you need for doing this kind of things. The function you're modifying has plenty of hooks available. Also see: Actions and filters are NOT the same thing….

The solution is to create your own plugin *:

<?php
/* Plugin name: My first plugin */

add_action( 'wp_insert_post', 'callback_so_17530930', 10, 2 );

function callback_so_17530930( $post_ID, $post )
{
    // Adjust for the desired Post Type
    if( 'page' !== $post->post_type )
        return;

    include '../push/send_message.php';
    sendNotification( $post->post_title, $post->guid );
}

* See Where do I put the code snippets I found here or somewhere else on the web?

Just drop the plugin PHP file inside wp-content/plugins folder. Go to the dashboard plugins' page and activate. More details at the Codex.

4
  • ok, i add plugin and i've activated it. I put in your code, inside callback function, a simple print_r($_POST);. The problem is that i see that only when i click on "create new article" and not when i publish it. How can i edit to achive what i need?
    – Jayyrus
    Commented Jul 8, 2013 at 20:27
  • Use the save_post action hook. Check this Answers for working examples.
    – brasofilo
    Commented Jul 8, 2013 at 20:31
  • if you see the answer above yours, @s_ha_dum says me to use save_post. Using it the result is the same as yours
    – Jayyrus
    Commented Jul 8, 2013 at 20:33
  • I think the problem you posted is covered in both answers: use hooks for it to work. Now you're working with a different code. As I said, research within the working examples. To debug this specific hooks you need to die() after print_r()/var_dump(), or use FirePHP or similar.
    – brasofilo
    Commented Jul 8, 2013 at 20:39

Your Answer

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

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