As mentioned previously, I installed the User Permissions Plugin (version 0.8.4) so that I could control who gets to read my more personal posts. The plugin was working great until I realized that it was denying access to posts that it should not be.
First I had to figure out how to debug WordPress plugins. I first tried the wp pear debug plugin but I could not figure out how to get it to output anything. Later I realized it was making things worse because for some reason it cleared all the roles from the current user. Then I found the WiPeD debug plugin which was simple to use.
After many hours of debugging I determined that the problem was an incompatibility between the User Permissions Plugin and the Yet Another Related Posts Plugin (YARPP). The problem was that if a public post had at least one related post that was protected the User Permissions Plugin would redirect the user away from the public post.
The simple solution was to not redirect if the post that is protected is not the post being displayed. The trick was finding the post ID but fortunately this article, Retrieve and Get WordPress Post ID Outside the Loop as PHP Variable, turned out to be immensely helpful.
It turns out there are three different ways to get the post ID.
- global $wp_query;
$post_id = $wp_query->post->ID;
- global $post;
$post_id = $post->ID;
- global $id;
$post_id = $id;
Using this information I modified user.permissions.php in the User Permissions Plugin source and now this plugin works happily with YARPP. Here are the modifications I made. I sent these modifications to the plugin author, John Godley.
***************
*** 90,109 ****
{
if (!empty ($posts))
{
! global $current_user, $id;
! foreach ($posts AS $idx => $post)
{
$permissions = Permissions::get ($post->ID);
if ($permissions && $permissions->can_read ($current_user) == false)
{
! if (is_single () || is_page ()) {
! if (empty($id) || $id == $post->ID) {
! $permissions->redirect ();
! }
! }
else
! unset ($posts[$idx]);
}
}
--- 90,106 ----
{
if (!empty ($posts))
{
! global $current_user;
! foreach ($posts AS $id => $post)
{
$permissions = Permissions::get ($post->ID);
if ($permissions && $permissions->can_read ($current_user) == false)
{
! if (is_single () || is_page ())
! $permissions->redirect ();
else
! unset ($posts[$id]);
}
}
Tags: wordpress
Categories: Web
6 Comments »
Recent Comments