Why didnt wordpress include parameter in the save_post action refernce?

http://codex.wordpress.org/Plugin_API/Action_Reference/save_post

Why WordPress didnt include $post and $post_ID parameter and their function. I realise these parameter exist only when I checked the source code, What these parameter actually do??
Don’t you think that they need to include every parameter in the action hook reference so that we can assign action to the hook

Related posts

Leave a Reply

2 comments

  1. $post holds the post object that is being saved.
    $post_ID holds the ID of the post that is being saved.

    You can get these parameters in your callback function when making your add_action call.

    example

    add_action( 'save_post', 'my_save_function', 10, 2 );
    

    The 10 represents priority – 10 is default, the lower the number, the higher the priority (i.e. run earlier).

    The ‘2’ in the last parameter is the number of arguments to pass to your callback. The default is 1, but as you state there can be 2 parameters here so you can up the value to get them both, if they are both needed.

    function my_save_function( $post, $post_ID ) {
    
       // Do stuff here.
    
    }
    

    There are lots of actions/filters with variable numbers of parameters that aren’t known implicitly, so doing this way allows the code to be flexible.

    In answer to why it wasn’t included on the save callback – it’s not needed in that instance. The $post object provides a simple means of accessing the ID. You might want to include it in your own hooks – perhaps if you’re altering the object for whatever reason, but really there are endless possibilities.

    It’s always best to do what you have done and check core for things like this, understanding what core is up to, makes developing with WordPress a lot easier 🙂

  2. class myObj {
     function on_save_post($post_ID, $post)
     {
      // process here
     }
    }
    add_action('save_post', array('myObj', 'on_save_post'));
    

    Where this class myObj is that you’ll have all the parameters you need, so you can pass all the variables you want within save_post