#51291 closed enhancement (fixed)
Add filter to show post list table `column_cb` even without `edit_post` capability
Reported by: | coreyw | Owned by: | johnbillion |
---|---|---|---|
Milestone: | 5.7 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Posts, Post Types | Keywords: | good-first-bug has-patch has-dev-note |
Focuses: | administration | Cc: |
Description
Right now the checkbox in the posts list table only shows if the user can edit the post. I am working on a plugin that allows posts to be archived, which means they are not visible publicly, and are no longer editable (until unarchived).
To do this, I have used the map_meta_cap
filter to disallow the edit_post
capability if the current post_status
is archive
. This is great because the post in the table no longer has a link to edit or quick edit, and if you use a direct URL to edit the post a message shows to user saying they are not allowed (having a custom message here that says the post is archived would be good, maybe there is already an easy way to do so?).
The problem though, is that the checkbox also is gone in the list table. This does not allow a user to select archived posts and then use the "Unarchive" bulk action.
Currently to get around this, I am looping over debug_backtrace()
to check if the current_user_can('edit_post', $id)
call is coming from the column_cb
function or not and returning the appropriate value. Obviously this is not preferred though.
There may be a better way than simply allowing a filter for the column_cb
as well, but hopefully this explains the end goal.
Change History (15)
#1
@
4 years ago
- Component changed from Administration to Posts, Post Types
- Focuses administration added
- Keywords needs-patch good-first-bug added
This ticket was mentioned in PR #858 on WordPress/wordpress-develop by alexstine.
4 years ago
#2
- Keywords has-patch added; needs-patch removed
This ticket was mentioned in PR #858 on WordPress/wordpress-develop by alexstine.
4 years ago
#3
#4
@
4 years ago
- Keywords needs-testing added; has-patch removed
- Owner set to alexstine
- Status changed from new to accepted
Hello @coreyw
I have linked a PR to this ticket. I've suggested a filter that if set to true, the checkbox will output regardless of capability check. Is this what you were after?
Can I also get your feedback @johnbillion ? Happy to alter or adjust if it's not quite right.
This filter can be added to child theme or plugin.
add_filter( 'bypass_cap_check_on_post_cb', '__return_true' );
Testing welcome.
Thanks.
#6
@
4 years ago
- Milestone changed from Awaiting Review to 5.7
- Owner changed from alexstine to johnbillion
#7
@
4 years ago
This all looks good @alexstine, just two points:
- The filter needs a better name. Probably something along the lines of
show_post_checkbox
? - Let's pass the
$post
object to the filter for additional context
#8
@
4 years ago
@johnbillion Looking better now?
I gave the filter a better name that more closely reflects the area, hopefully it's not too specific. I also added the $post parameter as a 2nd arg.
#9
@
4 years ago
I just had a thought. If the result of the current_user_can
check is used as the default value of the filter (instead of being used in place) then the filter can be used both to forcibly enable the checkbox when needed and to forcibly disable it when it isn't needed.
Example:
$show = current_user_can( 'edit_post', $post->ID ); /** ... */ if ( apply_filters( 'wp_list_table_show_post_checkbox', $show, $post ) ) :
@alexstine What do you think?
#10
@
4 years ago
@johnbillion Makes sense. current_user_can() returns true/false, so perfect way to use for this situation.
Updated the PR.
johnbillion commented on PR #858:
4 years ago
#12
#13
@
4 years ago
Thanks @alexstine! And @johnbillion said exactly what I was thinking as well. Looks good to me now.
#14
@
4 years ago
- Keywords needs-dev-note added; needs-testing removed
Perhaps it would be nice to mention this change in the Miscellaneous Changes dev note. Tagging this has needs-dev-note
to see if it's worth to mention.
Yes please. I've had a need for this too.