I've tried to achieve it by using snippet from years ago, but it doesn't seem to work. I need to make it work with Gutenberg (WP 5.0).

Is it possible to make it work?


2 Answers 2


The other suggestions (and the accepted answer in your link) temporarily change a user's capabilities globally. That is a hack. There is hook specifically designed for conditionally adjusting capabilities for specific content: map_meta_cap.

When WordPress checks whether a user can edit a post, it checks if the user can edit_post. WordPress decides which actual capability users have that this maps to using the map_meta_cap() function.

For example, when checking if a user can edit a post, it checks if the post was authored by the current user. If it was, then it maps the 'meta capability' edit_post to the 'primitive capability' edit_posts. If the post was authored by someone else it maps it to edit_others_posts. Then it checks if the current user has the mapped capability.

So we can hook into this process so that whenever WordPress maps edit_post we will check if the current user is a Contributor, and if the post is older than 12 hours. If both those things are true we will map edit_post to do_not_allow, meaning that the user will not be allowed to edit it:

function wpse_319901_contributor_can_edit( $caps, $cap, $user_id, $args ) {
    // Stop if this isn't a check for edit_post or delete_post.
    if ( $cap !== 'edit_post' || $cap !== 'delete_post' ) {
        return $caps;

    // Get the current user's roles.
    $user  = get_userdata( $user_id );
    $roles = $user->roles;

    // Stop if the user is not a Contributor.
    if ( ! in_array( 'contributor', $roles ) ) {
        return $caps;

    // For edit_post the post ID will be the first argument in $args.
    $post = get_post( $args[0] );

    // Is the post older than 12 hours?
    if ( get_the_time( 'U', $post ) < strtotime( '-12 hours' ) ) {
        // If so, do not allow the user to edit it.
        $caps[] = 'do_not_allow';

    return $caps;
add_filter( 'map_meta_cap', 'wpse_319901_contributor_can_edit', 10, 4 );

You can read more about capabilities and how meta capabilities are mapped to primitive capabilities here.


How to disable edit post option after period of time? Similar to this explanation you pointed out:

It's preferable to use get_post_time() instead accessing the global variable $post->post_date. This is really ugly and bad practice.By default it formats to UNIX EPOCH but to be safe pass the U argument.

PHP has a built in function date('U') also in UNIX EPOCH

function disable_editing_after_twelvehours( $post_object ) {
    $currentTime = date('U');
    $postTime = get_post_time('U',false,$post_object->ID,false);

    $current_user = wp_get_current_user();
        /*Subtract current time from post time and check if it is greater 
        12hrs(43200 seconds)*/
        if(($currentTime - $postTime) > 43200 {
              $current_user->cap[0] = false;
add_action( 'the_post', 'disable_editing_after_twelvehours' );

hooking it to the post should enable it to check all posts that load. Let me know if you have problems, as I wrote this really quick before losing my train of thought and getting busy.

  • Thank you so much! Unfortunately, it disables editing posts after 12 hours to all users if I'm correct. I need this function to work only for contributors. Could you please modify it a bit when you will have some time?
    – kacper3355
    Nov 22, 2018 at 2:10
  • see if that helps. Nov 22, 2018 at 3:06
  • the_post is a very strange hook to use for this... Nov 22, 2018 at 3:57
  • I suggest you actually give feedback. Nov 22, 2018 at 4:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.