latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Remove ability to login

Home Forums Community Forum Remove ability to login

This topic contains 3 replies, has 3 voices. Last updated by  Epix Media 3 years, 11 months ago.

Topic Author Topic
Posted: Wednesday Jan 16th, 2013 at 8:49 am #37719
Epix Media
Username: epixmedia

Hi all. I’m building another website with s2member and need to impliment it slightly differently this time.

My client only wants their members to be able to login and edit their details at specific times of the year. Is there a way we can block & unblock logging in for a specific member level? This would be my ideal solution.

My alternative is to simply remove their ability to edit their profile until updating is re-enabled!

Thanks!!

Zoe

List Of Topic Replies

Viewing 3 replies - 1 through 3 (of 3 total)
Author Replies
Author Replies
Posted: Thursday Jan 17th, 2013 at 3:16 am #37862
Bruce
Username: Bruce
Staff Member

Thanks for your great question.

You may want to check out the wp_logout() function. What you can do is update your Login Welcome Page to reflect that you may not currently log in, and use the above function with the conditionals available in Dashboard -› s2Member® -› API / Scripting -› Advanced PHP Conditionals to decide when to log a user out.

Note that to log a user out after the page a loaded, you should use the wp_footer hook.

Posted: Thursday Jan 17th, 2013 at 10:54 pm #38002
Staff Member

Thanks for the heads up on this thread :-)

The wp_authenticate function is what logs a user into WordPress. Luckily, WordPress makes this a pluggable function. Meaning, you can define it yourself, and WordPress will use your version of the function instead of it’s default version.

Here is the default version of the function in WP v3.5.

/**
 * Checks a user's login information and logs them in if it checks out.
 *
 * @since 2.5.0
 *
 * @param string $username User's username
 * @param string $password User's password
 * @return WP_Error|WP_User WP_User object if login successful, otherwise WP_Error object.
 */
function wp_authenticate($username, $password) {
	$username = sanitize_user($username);
	$password = trim($password);

	$user = apply_filters('authenticate', null, $username, $password);

	if ( $user == null ) {
		// TODO what should the error message be? (Or would these even happen?)
		// Only needed if all authentication handlers fail to return anything.
		$user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));
	}

	$ignore_codes = array('empty_username', 'empty_password');

	if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
		do_action('wp_login_failed', $username);
	}

	return $user;
}

A modified version might look something like this.

Please create this directory and file:
/wp-content/mu-plugins/s2-hacks.php
(NOTE: these are MUST USE plugins, see: http://codex.wordpress.org/Must_Use_Plugins)
(See also: http://www.s2member.com/kb/hacking-s2member/)

/**
 * Checks a user's login information and logs them in if it checks out.
 *
 * @since 2.5.0
 *
 * @param string $username User's username
 * @param string $password User's password
 * @return WP_Error|WP_User WP_User object if login successful, otherwise WP_Error object.
 */
function wp_authenticate($username, $password) {
	$username = sanitize_user($username);
	$password = trim($password);

	$user = apply_filters('authenticate', null, $username, $password);

	if ( $user == null ) {
		// TODO what should the error message be? (Or would these even happen?)
		// Only needed if all authentication handlers fail to return anything.
		$user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));
	}
	else if($user->has_cap('s2member_level1')) // A Level #1 Member?
		{
			$deny = TRUE; // Deny these Members?
			
			if($deny)
				{
					$user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: You have been denied access at this time. Please try again later.'));
				}
		}

	$ignore_codes = array('empty_username', 'empty_password');

	if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
		do_action('wp_login_failed', $username);
	}

	return $user;
}
Posted: Friday Jan 18th, 2013 at 4:38 am #38014
Epix Media
Username: epixmedia

Fab! Thanks Jason :)

Viewing 3 replies - 1 through 3 (of 3 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.