themosis / framework

The Themosis framework core.
https://framework.themosis.com/
GNU General Public License v2.0
671 stars 121 forks source link

Add custom post type hook on save/publish/update action #129

Closed jlambe closed 9 years ago

jlambe commented 9 years ago

As there is no access to each post type save_post action function, provide a hook for each custom post type so developers can run custom code when publishing/updating a post.

jlambe commented 9 years ago

2 new hooks for custom post type:

  1. Action before save: Action::add('themosis_postTypeName_BeforeSave')
  2. Action after save: Action::add('themosis_postTypeName_AfterSave')

Both actions will pass 3 arguments:

  1. The post ID
  2. The custom post type name
  3. An array of registered custom fields (array of Field instances)

So if we have a custom post type with a name of prefix-cars:

$cars = PostType::make('prefix-cars', 'Cars', 'Car')->set();

// Run code just before saving car post type data.
Action::add('themosis_'.$cars->get('name').'_BeforeSave', 'CarsController@before');

// Run code right after saving car post type data.
Action::add('themosis_'.$cars->get('name').'_AfterSave', 'CarsController@after');
jlambe commented 9 years ago

This is going to be removed finally as there is already by default a save_post_{$post_type} action hook available by WordPress.