WordPress制作会员中心需要注意的几个问题(非插件)

虽然wp自带了会员系统,但是始终是在后台显示,这让人很不习惯,使用也不方便,整合到前台操作,让会员在前端界面编辑,上传,发布也是很惬意的事情……

目前很多wordpress模板都带上了会员中心了,但构造到前台的会员系统也是需要注意很多事情的…… 下面列出一些需要注意的事项:

一、WordPress禁止用户上传特定类型的文件

主题的functions.php中插入以下php代码:

add_filter('upload_mimes', 'custom_upload_mimes');
function custom_upload_mimes( $existing_mimes=array() ) {
	// 注意中括号中的名称,必须取自上面支持列表中中括号的名称
	unset( $existing_mimes['exe'] ); //此处禁止了上传exe后缀名的可运行文件
	unset( $existing_mimes['jpg|jpeg|jpe'] ); //此处禁止了上传jpg、jpeg和jpe后缀名的压缩文件
	unset( $existing_mimes['gif'] ); //此处禁止了上传gif后缀名的图片文件
	unset( $existing_mimes['png'] ); //此处禁止了上传png后缀名的图片文件
	return $existing_mimes;
}
二、让WordPress只支持上传图片

如何让WordPress只支持上传图片文件,其他文件一概拒绝上传。方法很简单,在当前主题的functions.php中插入以下php代码即可:

Add the filteradd_filter('upload_mimes', 'custom_upload_mimes');
function custom_upload_mimes( $existing_mimes=array() ) {
	$existing_mimes = array('jpg|jpeg|jpe' => 'image/jpeg','gif' => 'image/gif','png' => 'image/png','bmp' => 'image/bmp','tif|tiff' => 'image/tiff','ico' => 'image/x-icon');
	return $existing_mimes;
}
三、让WordPress的默认角色用户无法进入后台

如果你不想让默认角色的用户进入WordPress后台乱逛,你可以在当前主题的functions.php中加入以下代码,然后使用默认角色的用户帐号登录

if ( is_admin() && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) ) {
	$current_user = wp_get_current_user();
	if($current_user->roles[0] == get_option('default_role')) {
		wp_safe_redirect( home_url() );
		exit();
	}
}
四、让WordPress支持用户名或Email登录
//让WordPress支持用户名或邮箱登录
function dr_email_login_authenticate( $user, $username, $password ) {
	if ( is_a( $user, 'WP_User' ) )
		return $user;
 
	if ( !empty( $username ) ) {
		$username = str_replace( '&', '&', stripslashes( $username ) );
		$user = get_user_by( 'email', $username );
		if ( isset( $user, $user->user_login, $user->user_status ) && 0 == (int) $user->user_status )
			$username = $user->user_login;
	}
 
	return wp_authenticate_username_password( null, $username, $password );
}
remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
add_filter( 'authenticate', 'dr_email_login_authenticate', 20, 3 );
 
//替换“用户名”为“用户名 / 邮箱”
function username_or_email_login() {
	if ( 'wp-login.php' != basename( $_SERVER['SCRIPT_NAME'] ) )
		return;
 
	?><script type="text/javascript">
	// Form Label
	if ( document.getElementById('loginform') )
		document.getElementById('loginform').childNodes[1].childNodes[1].childNodes[0].nodeValue = '<?php echo esc_js( __( '用户名/邮箱', 'email-login' ) ); ?>';
 
	// Error Messages
	if ( document.getElementById('login_error') )
		document.getElementById('login_error').innerHTML = document.getElementById('login_error').innerHTML.replace( '<?php echo esc_js( __( '用户名' ) ); ?>', '<?php echo esc_js( __( '用户名/邮箱' , 'email-login' ) ); ?>' );
	</script><?php
}
add_action( 'login_form', 'username_or_email_login' );
五、让wordpress支持中文用户名的简便方法

将以下php代码复制到当前主题目录下的functions.php中,让WordPress支持使用中文用户名注册和登录:

function ludou_non_strict_login( $username, $raw_username, $strict ) {
	if( !$strict )return $username;
	return sanitize_user(stripslashes($raw_username), false);
}
add_filter('sanitize_user', 'ludou_non_strict_login', 10, 3);
六、WordPress用户注册成功后自动登录

让用户注册成功后自动登录,并跳转到指定页面,即让用户省了手动登录这一步,又提高了用户体验。实现起来很简单,我们可以在当前主题的functions.php 的第一个<?php 下面添加以下php代码:

// 用户注册成功后自动登录,并跳转到指定页面
function auto_login_new_user( $user_id ) {
	wp_set_current_user($user_id);
	wp_set_auth_cookie($user_id);
	// 这里设置的是跳转到首页,要换成其他页面// 可以将home_url()改成你指定的URL
	// 如 wp_redirect( 'https://haida123.com' );
	wp_redirect( home_url() );
	exit;
}
add_action( 'user_register', 'auto_login_new_user' );
七、WordPress退出后跳转到指定页面

这个问题也很好解决,将下面的php代码放到当前主题的functions.php中即可:

add_filter('logout_url', 'ludou_logout_redirect', 10, 2);
function ludou_logout_redirect($logouturl, $redir) {
	$redir = 'https://haida123.com/'; // 这里改成你要跳转的网址
	return $logouturl . '&amp;redirect_to=' . urlencode($redir);
}
标签
THE END
喜欢就支持一下吧

相关推荐

评论

抢沙发
G
Guest
No Comment
There's nothing here!