Whew, that one was hard to solve ;-) However, the thing is, you have to dig deep here.
The function current_user_can()
uses a lot of different functions afterwars, but you can filter user_has_cap
, which basically checks if the logged in user has a specific capability (duh).
As contributors are allowed (by default) to edit posts with a status other than publish
(please correct me if I am wrong, i did not check for trash
), and also the capabilites are checked multiple times while loading the admin screen, we have to check a few different things before returning an error:
- Can the user
edit_posts
in the first place?`
- Is the status of the post to be edited
future
?
- Does the user want to edit the post or create a new one, and is a post set by the url? (probably the least important due to the post status).
The (almost) solution
If no post (by $_GET['post']
) is set, the filter can continue without restriction.
Afterwards, you have to check if the post_status
is indeed future, if the action (by $_GET['action']
) is really 'edit', and, of course, if the current user is allowed to edit in the first place.
add_filter( 'user_has_cap', 'f711_restrict_editing_future', 1, 3 );
function f711_restrict_editing_future ( $allcaps, $cap, $args ) {
// check if a post ist set
if ( !isset( $_GET['post'] ) ) return $allcaps;
// get the post
$thispost = get_post( (int) $_GET['post'] );
// check everything else
if ( $cap[0] == 'edit_posts' && $thispost->post_status == 'future' && $_GET['action'] == 'edit' && $allcaps['contributor'] == 1 ) {
wp_die( 'post is scheduled' );
}
return $allcaps;
}
The Problems
This function to filter your user role just checks at the time the user wants to edit the future post. You may have to filter the lists of the posts too - to avoid displaying the link to the post in the first place.
The second issue is, this solution depends on the user having the role of the contributor
. You may have to adjust these settings as well, if you want to have a general solution for this problem.
However, to anser your question - with this function you prevent contributors to edit future posts
Not the most elegant way, but I hope you get the twist :-)
post_status
do the posts have, after they were scheduled? Are theyfuture
?