• Resolved dynamitharry

    (@dynamitharry)


    Hey!

    I tried to find a way to limit access to a page by the username, so far, the only way I found out is by user role.

    Anyone that knows a plugin or anything that could do this?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter dynamitharry

    (@dynamitharry)

    I made some tests with custom fields. I set a name in a custom field in the page, the name of the user who should be able to see the page. Then i compare this with the logged in user and also i make so the admin always can see the page.

    Someone that has any thoughts about this and security. Is it a safe way of doing it?

    <?php global $current_user;
          get_currentuserinfo();
          $username = $current_user->user_login;
    		 $user = get('user_for_page');
     if (($username == $user) || (current_user_can('manage_options'))) { ?>
    Thread Starter dynamitharry

    (@dynamitharry)

    Ok, since i don’t get any responses here i have made my own solution that i gladly share.

    First thing, i’m using Thematic with a child theme, and the plugin Magic Fields. But i guess that does not matter.

    I have created two page templates. One called login and one called logged in page.

    The login page is the page where i login. It goes a bit backwards but but i take it from top to bottom anyway.

    <?php
    /**
     * Template Name: Custom Login Page
     *
     */
     //get the url from the current user
     	global $current_user;
          get_currentuserinfo();
          $address = $current_user->user_url;
          //when the page relodes direct the user to the user url address..
     if ( is_user_logged_in() ) { header( 'Location:' . $address ) ; } 
    
    wp_login_form();

    When you visits it and is not logged in, you will see the form. when you log in and the page reloads you will get directed to the your user url.

    Then to the “logged in page”, you have to create this. Set a custom field to have the same value as the user name. Then the code goes like this:

    //Check if the user is logged in
    	   global $current_user;
          get_currentuserinfo();
          //get the current username of the logged in user.
          $username = $current_user->user_login;
          // get the custom value user name of the page
    		 $user = get('restricted_to_user');
    		 //check if the username for the page and the username is the same. And always let the admin see the page..
     if (($username == $user) || (current_user_can('manage_options'))) {

    then go on the way you want with the page and in the end of the page:

    <?php } else {
    
       header( 'Location: https://localhost:8888/?page_id=XXX' ) ;
    }
    ?>

    so that if you are not logged in you get directed to the login page.

    This is quite manual, but still, i don’t have to mess with plugins, roles and stuff. It feels great.

    If anyone have any doubts about this regarding security, searchbots or anything please tell me, because i don’t know much about it..

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘limit page access by username’ is closed to new replies.