If a post is protected and the user does not have permission to view the post then the User Permissions Plugin redirects the user to the index page of the website, i.e. “/”.

On my blog I had previously updated this plugin so that the user is redirect to the Protected Posts page.  I have since updated the plugin to redirect to the login page if the user does not have permission to view the post.  This is because on my blog if a user does not have permission it is most likely because she is not registered and/or not logged in.  Once the user logs in she will be redirected to the post she intended to view.

To figure out how to do this I followed several steps.

  1. Figure out what properties of the post were available to me.To do this I used the new Reflection API in PHP 5 to find this information.
    Reflection::export(new ReflectionObject($post))

    It turned out that there was a page describing all the post properties available in the API.

  2. Figure out how to get information about the blog itself.  Fortunately there is a simple get_bloginfo template tag.  I used this to get the URL of the blog.
  3. Figure out how to create the login URL and redirect to that URL.  I determined that though my URL permalink structure is site-url/year/month/day/post-name, e.g. http://www.kimplicity.com/fkim/blog/2009/06/12/sample-post/, that if I gave a URL of the structure site-url/post-name that it would properly redirect.  But then I determined that an even better solution would be to use the URL site-url?p=ID where ID is the post ID.

Here are the modifications I made.

*** user-permissions.php
***************
*** 98,104 ****
  {
    if (is_single () || is_page ()) {
      if (empty($id) || $id == $post->ID) {
!       $permissions->redirect ();
      }
    }
    else
--- 98,104 ----
  {
    if (is_single () || is_page ()) {
      if (empty($id) || $id == $post->ID) {
!       $permissions->redirect ($post->ID);
      }
    }
    else
*** permissions.php
***************
*** 155,163 ****
!       function redirect ()
  {
    if ($this->redirect_read > 0)
      wp_redirect (get_permalink ($this->redirect_read));
!   else
!     wp_redirect ('/fkim/blog/2009/05/28/protected-posts/');
    die ();
  }
}
--- 155,166 ----
!       function redirect ($postid)
  {
    if ($this->redirect_read > 0)
      wp_redirect (get_permalink ($this->redirect_read));
!   else {
!     $site_url = get_bloginfo('siteurl');
!     $login_url = $site_url . '/wp-login.php?redirect_to=' .
!                  $site_url . '/?p=' . $postid;
!     wp_redirect ($login_url);
!   }
    die ();
  }
}
Share

Related posts:

  1. Getting User Permissions to work with YARPP
  2. Protected Posts
  3. WordPress Theme Switcher Plugin and Widget Not Working
  4. Configuring Vista for Automatic Login
  5. Related and Popular Posts in WordPress

No Responses to “Redirect to Login in User Permissions Plugin”

Care to comment?