WordPress.org

Ready to get started?Download WordPress

Forums

save_post not working getting called.... (13 posts)

  1. PSG2011
    Member
    Posted 3 years ago #

    To do some experiment with save_post hook, i wrote following bare minimum code. But looks like it is not getting called consistently.

    I am just echoing html comment in the call back. So after GUI action if we do view source i should see my comment.

    I see my comment being printed on when I try "new post". If publish post and then do view source I do NOT see my html comment.

    Any idea why my call back is not getting called consistently?

    <?php
    /*
    Plugin Name: Save Post
    Plugin URI: http://wordpress.org/
    Description: Save Post Example.
    Author: wordpress.org
    Version: 1.0
    Author URI: http://wordpress.org/
    */

    /* Do something with the data entered */
    add_action('save_post', 'myplugin_save_postdata');

    /* When the post is saved, saves our custom data */
    function myplugin_save_postdata( $post_id ) {
    echo "<!------------------ Test Comment --------------->";
    return $post_id;
    }

    ?>

  2. markowe
    Member
    Posted 3 years ago #

    I see this never got answered. I have the same problem - tried the same thing, echoing some debug code in the save_post callback and it only shows on New post (presumably when the first autodraft is saved, or the database entry created for the new post). Ever find a solution?

  3. jonbyrd
    Member
    Posted 3 years ago #

    Probably to late, but the answer should help others if not you guys.

    I've had the same problem with save_post not getting called. Although the programming looks as if save_post should be called on an update of a post, something's probably hooking in and redirecting the logic.

    You can use 'edit_post' AS WELL as 'save_post', here's the code example:
    do_action('edit_post', $post_ID, $post);

  4. markowe
    Member
    Posted 3 years ago #

    Thanks Jon, not late at all :)

    Hmm, I tried edit_post too and nothing seemed to happen at all but I will look into that again. I think my concern with edit_post was that this action is hooked when comments are made too. Because I am using save_post (or not, as is the case currently) to save POSTed meta_box data on the edit post/page I definitely don't want that happening when a user makes a comment - I would have to do additional checks to make sure we are on an edit post/page screen, not even sure how to do that right now.

    OK, thanks, will look into edit_post again and see if there are any reasons why it might not be working.

    BTW, what's with those parameters? Would it be the same for add_action?

    I was doing:
    add_action (edit_post, array(&$mypluginclassinstance, 'mycallback'));
    Something seems to be out-of-date in the docs about that...

  5. rob.langley
    Member
    Posted 3 years ago #

    I'm also having similar issues, had written some simple code like your self as I suspected these were not firing. I have tried.

    //save_post only fires when you create a new post NOT the first save / publish
    add_action('save_post', 'save_postdata');
    
    // never seems to fire
    add_action('edit_post', 'edit_postdata');
    
    //never seems to fire
    add_action('publish_post', 'publish_postdata');
    
    // fires after save_post above
    add_action('wp_insert_post', 'insert_postdata');

    I have written some custom write panels for my client as detailed in http://wefunction.com/2008/10/tutorial-creating-custom-write-panels-in-wordpress/ but can't get them to save :-(

    Any ideas

    Rob

  6. markowe
    Member
    Posted 3 years ago #

    Well, I got things working finally - I had to pry into the code of another plugin (All-In-One SEO) to see how they had done it, and this was what worked for me:

    add_action('edit_post', array ( $dl_myplugin, 'myplugin_save'));
    add_action('save_post', array ( $dl_myplugin, 'myplugin_save'));
    add_action('publish_post', array ( $dl_myplugin, 'myplugin_save'));
    add_action('edit_page_form', array ( $dl_myplugin, 'myplugin_save'));

    My needs were simpler - to save the meta_box data regardless of whether the post is being published, saved, autosaved, or whatever. This seems a bit spray-and-pray, as I am not really sure which hook is being activated here but I guess I covered all eventualities! Hope this helps!

  7. markowe
    Member
    Posted 3 years ago #

    P.S. This edit_page_form isn't documented in the Codex other than a single line here http://codex.wordpress.org/Plugin_API/Action_Reference where it says:

    Runs just before the "advanced" section of the page editing form in the admin menus.

    and I have not yet tested whether I need edit_form_advanced too, since this says:

    Runs just before the "advanced" section of the post editing form in the admin menus

    I.e. the first is for pages, the second is for posts - I need it working on both, will look into that once I have got my code working on the page edit form - one thing at a time!

  8. rob.langley
    Member
    Posted 3 years ago #

    THANKS !!! edit_page_form is the only hook that gets called when I save a page. Now I can get on with the real code :-)

  9. markowe
    Member
    Posted 3 years ago #

    Great! I am glad we got this fixed between us! But it does show that the add_meta_box function reference is out of date since it only mentions save_post there. Not sure at what point these actions got split up (I am guessing that's what happened?) but other plugin authors seemed to figure it out, not sure how!

  10. PSG2011
    Member
    Posted 3 years ago #

    I have not tried the workarounds that markowe suggested. But for the time being I have switched to "WPAlchemy" (http://www.farinspace.com/wpalchemy-metabox/).

    Also if you see code for "WPAlchemy" you will notice that it works perfectly fine with just "save_post" and nothing more. (So there is some magic which we are not aware of)

  11. rmlumley
    Member
    Posted 3 years ago #

    I'm having a similar issue. Save_Post seems to only be called when I'm publishing a post or updating a previously published post. If I'm in draft mode and click Save, it's doesn't seem to be calling it.

    http://wordpress.org/support/topic/metabox-saves-on-update-or-publish-but-not-on-saving-draft?replies=1

  12. guldi
    Member
    Posted 2 years ago #

  13. Gonçalo Peres
    Member
    Posted 2 years ago #

    I think i found the "problem".

    the "save_post" action is only called when we actually changed something in the post page form. If we just press the update button, without changing anything, the "save_post" action is not called.

    This is important if we are editing a custom post type where we had custom meta boxes. If we rely on the "save_post" action and only change stuff on our custom meta boxes, nothing will happen.

    The solution is to use the "pre_post_update" action hook, instead of "save_post".

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags