Extending WP Help to users who can’t edit_posts

Yesterday I was explaining how I created a user role to edit a particular post type and only that post type. Today I want to show how you can use Mark Jaquith‘s excellent WP Help plugin to support these users. The issue I wanted to solve is that a freshly installed copy of WP Help only shows up for users with the edit_posts capability (i.e. any users who can access and edit posts on your WordPress site).

The key to solving our problem is provided by Mark in the form of a filter, cws_wp_help_view_documents_cap, through which we can allow pretty much anyone to view the help documentation. In the scenario yesterday, we created a custom post type with the associated edit_videos capability to control edit access, here’s how we can use this capability and the filter Mark provided to grant the access:

/**
 * Hooks the WP cws_wp_help_view_documents_cap filter 
 *
 * @param string $cap The capability required to view the help documents
 * @return string The capability required to view the help documents
 **/
function my_cws_wp_help_view_documents_cap( $cap ) {
	if ( current_user_can( 'edit_videos' ) ) // Obviously change edit_videos as you require
		return 'edit_videos';
	// Add more if statements, similar to the above, for
	// other capabilities here…
	return $cap;
}
add_filter( 'cws_wp_help_view_documents_cap', 'my_cws_wp_help_view_documents_cap' );

That’s it… drop an adapted version of the code above into your plugin or theme and you’re done.

Note that all users will be able to see all help documentation, so you may wish to write your help documentation with this in mind.

Caveat: This method is prone to (unintentional) disruption by someone executing on the cws_wp_help_view_documents_cap filter hook after you did, and changing the value you passed back. I can’t immediately think of a way around this, ideas welcome in the comments…

Thanks to Mark for the plugin!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.