php 我可以在 WordPress 中使用什么操作来在保存或更新自定义帖子时触发?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5151409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:57:37  来源:igfitidea点击:

What action can I use in WordPress that triggers whenever a custom post is saved or updated?

phpwordpresscustom-post-type

提问by Kyle Hotchkiss

Any way to have save_post for custom posts only? The way my functions.php is coded is tacking on lots of custom fields to normal posts and pages who don't need/use them.

有什么方法可以让 save_post 仅用于自定义帖子?我的functions.php 的编码方式是将许多自定义字段添加到不需要/使用它们的普通帖子和页面上。

回答by mindctrl

WordPress 3.7 introduced a new way of handling this with the save_post_{$post_type}hook.

WordPress 3.7 引入了一种使用save_post_{$post_type}钩子处理此问题的新方法。

Let's say your custom post type is "member-directory". You can now run save_post on that post type only by using something like this:

假设您的自定义帖子类型是“成员目录”。您现在可以仅通过使用以下内容在该帖子类型上运行 save_post:

function my_custom_save_post( $post_id ) {

    // do stuff here
}
add_action( 'save_post_member-directory', 'my_custom_save_post' );

回答by Tom Auger

Updated since 3.7.0 - props @Baptiste for the reminder

自 3.7.0 起更新 - 用于提醒的道具 @Baptiste

3.7.0 introduced the "save_post_{$post->post_type}" hook, which will be triggered by the post type. This allows you to add an action specific to your custom post type (or "page" or "post" etc). This saves you one line of the below.

3.7.0 引入了 " save_post_{$post->post_type}" 钩子,它会被 post 类型触发。这允许您添加特定于您的自定义帖子类型(或“页面”或“帖子”等)的操作。这为您节省了以下一行。

The accepted method is to add an action on save_post_{post-type}(substituting your post type's slug for {post-type}in the example above). There are a number of checks you can / probably should still do within your action's callback, which I document in the example below:

接受的方法是添加一个操作save_post_{post-type}{post-type}在上面的示例中替换您的帖子类型的slug )。您可以/可能仍然应该在您的操作回调中进行许多检查,我在下面的示例中记录了这些检查:

from the Codex:

来自法典

/* Register a hook to fire only when the "my-cpt-slug" post type is saved */
add_action( 'save_post_my-cpt-slug', 'myplugin_save_postdata', 10, 3 );

/* When a specific post type's post is saved, saves our custom data
 * @param int     $post_ID Post ID.
 * @param WP_Post $post    Post object.
 * @param bool    $update  Whether this is an existing post being updated or not.
*/
function myplugin_save_postdata( $post_id, $post, $update ) {
  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times

  if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
      return;


  // Check permissions
  if ( 'page' == $post->post_type ) 
  {
    if ( !current_user_can( 'edit_page', $post_id ) )
        return;
  }
  else
  {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;
  }

  // OK, we're authenticated: we need to find and save the data

  $mydata = $_POST['myplugin_new_field'];

  // Do something with $mydata 
  // probably using add_post_meta(), update_post_meta(), or 
  // a custom table (see Further Reading section below)

   return $mydata;
}

If you are registering multiple custom post types and you would like to consolidate your save_post functionality into a single function, then hook on the generic save_postaction. But then remember to do your post type check within your function if there is any differences in how those post types save their data.

如果您正在注册多个自定义帖子类型,并且您希望将您的 save_post 功能整合到一个函数中,那么请使用通用save_post操作。但是,如果这些帖子类型保存数据的方式有任何不同,请记住在您的函数中进行帖子类型检查。

eg: if ( 'my-cpt-1' == $post->post_type ){ // handle my-cpt-1 specific stuff here ...

例如: if ( 'my-cpt-1' == $post->post_type ){ // handle my-cpt-1 specific stuff here ...