我可以在WordPress中使用什么动作来保存或更新自定义帖子? [英] What action can I use in WordPress that triggers whenever a custom post is saved or updated?

查看:79
本文介绍了我可以在WordPress中使用什么动作来保存或更新自定义帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

自3.7.0开始更新-提醒@Baptiste的道具

3.7.0引入了 save_post _ {$ post-> post_type} 挂钩,该挂钩将由帖子类型触发。这使您可以添加特定于自定义帖子类型(或页面或帖子等)的操作。这样可以为您节省下一行内容。

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.

可接受的方法是在 save_post_ {post-type} (在上面的示例中,将您的帖子类型的代码替换为 {post-type} )。您可以/可能仍应在操作的回调中进行许多检查,我在下面的示例中对此进行了记录:

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:

来自食典

/* 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;
}

如果您要注册多个自定义帖子类型,并且希望合并save_post功能合并为一个功能,然后钩上通用的 save_post 操作。但是,请记住,如果您的帖子类型保存数据的方式存在任何差异,请在函数中进行检查。

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_post action. 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.

例如: if ('my-cpt-1'== $ post-> post_type){//在这里处理my-cpt-1特定的东西...

这篇关于我可以在WordPress中使用什么动作来保存或更新自定义帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆