0

I have a WordPress site which doesn't require any login or user registration. However, I have just created a page on my website which I would like to restrict to users I have added myself. I would like each user to have their own specific credentials so I can track when/where they are logging in. For example, I want John Smith to be able to view my restricted page. So I would create a username J_Smith and a password pass4321!. Then he could login with these credentials and view the page.

I've looked at a few plugins to do this, but they only seem to restrict access to pages based on WordPress user permissions. The people I would register wouldn't have any access to the WordPress development, but would access the site via URL like everyone else who visits. Only they would be able to view this extra page as well if they have correct login credentials.

Is there a way to do this with plugins? I've looked at this plugin https://wordpress.org/plugins/restrict-user-access/ and this one https://wordpress.org/plugins/restrict-content/ with no luck.

My current WordPress theme functions.php file looks like this:

<?php
/**
 *  Envision Multipurpose Theme (2013) - ThemeForest
 *  functions.php which does and tells WordPress to load CloudFW and the theme.
 *
 *              (Ya-Settar, Ya-Gaffar, Ya-Fettâh)
 *  @author     Orkun GURSEL
 *              <ticket:    support.cloudfw.net>
 *              <email:     support@cloudfw.net>
 *              <twitter:   @orkungursel, @cloudfw>
 *
 *  @package    WordPress
 *  @subpackage CloudFw
 *  @subpackage Envision
 */
/** Globals */

global $cloudfw_start, $cloudfw_memory;
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$cloudfw_start = $time;
$cloudfw_memory = memory_get_usage();

/** Defines */
if ( !defined('TMP_PATH') )         define( 'TMP_PATH', get_template_directory() . '/' );
if ( !defined('TMP_URL') )          define( 'TMP_URL', get_template_directory_uri() );
if ( !defined('CLOUDFW_TMP_PATH') ) define( 'CLOUDFW_TMP_PATH', dirname(__FILE__) );

/**
 *  Load & Run CloudFw
 */
require( TMP_PATH.'/cloudfw/cloudfw.loader.php' );

closed as off-topic by cybmeta, Jacob Peattie, cjbj, Jack Johansson, Pat J Nov 20 '17 at 16:23

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Did you manage to implement my answer okay? Let me know if you have any questions. – Nikki Mather Nov 20 '17 at 13:11
  • @Nikki I'm going to work on implementing it today and will follow up if I have any questions. Thank you for being so helpful. – Alex F Nov 20 '17 at 13:13
1

You can do this without any plugins rather easily. If you're only going to have a single page that needs to be accessed with an account created by you, try creating a page template with something like the below. Note: you should create any new account as a subscriber.

<?php 

    /*
        Template Name: Private Page
    */

    get_header(); 
?>
<?php 
    if( current_user_can('subscriber') {  ?> 
             // stuff here
<?php } else { 
    echo do_shortcode('[frontend-login-form]');
} ?>

<?php get_footer(); ?>

We have a shortcode in the example above, which comes from the code below which you should add to your functions.php file.

function frontend_user_manager_init() {
    add_shortcode( 'frontend-login-form', 'frontend_login_form' );
}
add_action('init', 'frontend_user_manager_init');
/**
 * Print a login form or current user login
 *
 * @param array $atts An array of arguments
 * @return string The form mark-up or the current user login
 */
function frontend_login_form( $atts ){
    if( ! is_user_logged_in() ){
        $args = array(
            'echo'          => false,
            'remember'      => true,
            'redirect'      => get_permalink(),
            'form_id'       => 'loginform',
            'id_username'       => 'user_login',
            'id_password'       => 'user_pass',
            'id_remember'       => 'rememberme',
            'id_submit'     => 'wp-submit',
            'label_username'    => __( 'Username' ),
            'label_password'    => __( 'Password' ),
            'label_remember'    => __( 'Remember Me' ),
            'label_log_in'      => __( 'Log In' ),
            'value_username'    => '',
            'value_remember'    => false
        );
        $output = wp_login_form( $args );
    }else{
        $current_user = wp_get_current_user();
        $output = '<p>' . sprintf( __( 'Howdy %s' ), $current_user->user_login ) . '</p>';
        $output .= '<p>' . wp_loginout( get_permalink(), false ) . '</p>';
    }
    return $output;
}
// Process shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');

The subscriber role has very limited access i.e. only allows the user to maintain their profile, however if you don't want to allow this try adding the below code to your functions.php file.

add_action( 'init', 'blockusers_init' );
function blockusers_init() {
    if ( is_admin() && ! current_user_can( 'administrator' ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
        wp_redirect( home_url() );
        exit;
    }
}
  • could you tell me where I am supposed to specify the page name I want the login form to be on? – Alex F Nov 20 '17 at 15:53
  • Ideally you need to create a page template and insert the code as mentioned at the start, then select the page template on the page you want the form to be on. To 'make' a page template, simply create a file named something like page-private.php in the wp-content/themes/themename/ directory and insert the first set of code from above. Does that make sense? – Nikki Mather Nov 21 '17 at 15:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.