duancheng3042 2016-03-09 10:39
浏览 43
已采纳

管理视图中的Wordpress更新后挂钩

i need a hook for the moment when an admin updates a post. (Click on update button). After the post is successfully updated.

The reason is, i have to call a function to update something for another plugin.

Everything i tried so far, is not working.

add_action( 'save_post', 'wpse41912_save_post' );
add_action( 'edit_post', 'wpse41912_edit_post' );
add_action( 'transition_post_status', 'wpse41912_transition_post_status' );
add_filter( "edit_post_{$field}", 'filter_edit_post_field', 10, 2 );
add_action( 'admin_head-post.php', 'admin_head_post_editing' );
add_action( 'admin_head-post-new.php',  'admin_head_post_new' );
add_action( 'admin_head-edit.php', 'admin_head_post_listing' );

In Everything function i wrote this, and i didnt see the echo or the alert box.

echo "my_update_user_meta";
$text = "my_update_user_meta";
echo '<script type="text/javascript">alert("' . $text . '")</script>'; 

Edit: i was missing the 3,4th parameter. My Code now

add_action( 'save_post', 'mmx_save_post_action', 10, 3 );
function mmx_save_post_action( $post_id, $post, $update ) {
  if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) { // keine Aktion bei Autosave
    //autosave
  }else{
    //no autosave
    if ( is_admin() && current_user_can( 'manage_options' )  ) {
      //admin panel && permission ok
     //call function
    }
  }
} 
  • 写回答

1条回答 默认 最新

  • dua55014 2016-03-09 11:42
    关注

    When a post is updated there are some hooks that are fired:

    • 'pre_post_update' is an action fired just before the post is updated, the argument passed are 2: $post_ID and $data that is an array of all the other database colums of the post table
    • 'transition_post_status' is an hook fired on update, and pass 3 arguments: $new_post_status, $old_post_status and $post (object).
    • Then, there are other 2 transition hooks fired, but they are dynamic named, it means that the effective action fired depends on the old and the new post status. "{$old_status}_to_{$new_status}" and "{$new_status}_{$post->post_type}". First pass the only the post object as argument, the second pass the post id and the post object. Find documentation here.
    • 'edit_post' that pass 2 arguments: $post_ID and $post (object)
    • 'post_updated' that pass 3 arguments: $post_ID, $post_after (post object after the update), $post_before (post object before the update)
    • Another dynamic hook: "save_post_{$post->post_type}" that depends on post type, e.g. for standard posts is 'save_post_post' and for pages is 'save_post_page', this hook pass 3 arguments: $post_ID, $post (object) and $update that is a boolean (true or false) that is true when you perform an update, in fact this hook is fired also when a post is saved for first time.
    • 'save_post' that is fired both on update and on first saving, and pass the same 3 arguments of the previous hook.
    • 'save_post_{$post_type}' that is fired both on update and on first saving, and pass the same first 2 arguments of the previous hook.
    • Finally you have 'wp_insert_post', that is fired both on update and on first saving, and pass the same 3 arguments of the previous 2 hooks.

    These hook are fired every time a post is updated, both via admin pages in backend and via when updated "manually" using wp_update_post or wp_insert_post functions.

    When the post is updated using admin pages there are additional hooks fired, an example is 'update_post_redirect' or 'post_updated_messages'. (See this and this WPSE answers for usage examples).

    Note that if you want make use of some hooks argument, that isn't the first, one you have to explicitly declare it in add_action call.

    E.g. if you want to use the '$update' argument (that is the 3rd) of the 'save_post' hook you need to add 3 as $accepted_args param on add_action (see docs):

    // if you don't add 3 as as 4th argument, this will not work as expected
    add_action( 'save_post', 'my_save_post_function', 10, 3 );
    
    function my_save_post_function( $post_ID, $post, $update ) {
      $msg = 'Is this un update? ';
      $msg .= $update ? 'Yes.' : 'No.';
      wp_die( $msg );
    }
    

    Last note regard timing: you must be sure that add_action is called before the action is triggered, or it will do nothing.

    E.g. this code:

    wp_update_post( $post );
    add_action( 'save_post', 'my_function', 10, 3 );
    

    will do nothing, because the action is added after the hook is fired. Here is simple to recognize it, in real world code isn't always so.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?