如何使用WordPress开发会员网站:第2部分

最终产品图片
您将要创造的

在本系列的第一部分中,我们介绍了自定义注册和登录表单。 今天,我们将介绍如何向注册表单添加自定义字段。 我们将为Twitter句柄添加一个文本输入和一个复选框,用户需要在注册之前同意条款。 所需的工作可以分为三个部分:

  • 自己添加字段
  • 现场验证
  • 处理数据

我们还将非常简短地讨论设置外发电子邮件样式的最佳方法。 这意味着用户注册时将收到精美的品牌电子邮件。

在开始之前,请确保您的WordPress设置( “设置”>“常规” )中勾选了“ 任何人都可以注册

常规设置

让我们开始吧

WordPress的最大优点之一就是它为您提供操作和过滤器的方式 。 这些使我们能够挂钩事件或过滤内容,这使我们有机会优雅地扩展WordPress。

因此,使用register_form操作 ,让我们进入注册表单并添加我们的字段。 将以下函数复制到我们在本系列的第一部分中创建的admin.php中。

<?php

function tutsplus_register_form_edit() {
    $twitter_name = ( ! empty( $_POST['twitter_name'] ) ) ? trim( $_POST['twitter_name'] ) : ''; ?>
    <p>
        <label for="twitter_name">
        	<?php _e( 'Twitter name', 'sage' ) ?><br />
        	<input type="text" name="twitter_name" id="twitter_name" class="input" value="<?php echo esc_attr( wp_unslash( $twitter_name ) ); ?>" size="25" />
        </label>
    </p>

	<?php $terms = ( ! empty( $_POST['terms'] ) ) ? $_POST['terms'] : ''; ?>
    <p>
        <label for="terms">
        	<input type="checkbox" name="terms" id="terms" class="input" value="agreed" <?php checked( $_POST['terms'], 'agreed', true ); ?> />
        	<?php _e( 'I have read the terms and conditions', 'sage' ) ?>
        </label>
    </p>
    <?php
}
add_action( 'register_form', 'tutsplus_register_form_edit' );

基本上,我们会在注册表格中注入一些新字段。 使用的标记类似于本机字段的标记。 如果页面被重新加载,我们还希望保留这些值,因此我们将检查它们是否存在于$_POST超全局$_POST 。 您可能想知道为什么我们的Twitter标签包含在函数中: <?php _e( 'Twitter name', 'tutsplus' ) ?>

_e函数允许进行翻译-您可以在WordPress Codex中阅读有关它的更多信息。

很好,但是进行一些验证呢? 就目前而言,用户可以在其中放置任何内容,也可以将其留空。 让我们将字段设为必填字段,并确保文本字段仅包含常规字符以防止恶意攻击。 这次,我们将使用WordPress过滤器: registration_errors

<?php

function tutsplus_validate_registration( $errors, $sanitized_user_login, $user_email ) {
    if ( empty( $_POST['twitter_name'] ) || !empty( $_POST['twitter_name'] ) && trim( $_POST['twitter_name'] ) == '' ) {
		$errors->add( 'twitter_name_error', __( '<strong>ERROR</strong>: Please enter your Twitter name.', 'sage' ) );
	}
	if ( preg_match('/[^a-z_\-0-9]/i', $_POST['twitter_name']) ) {
		$errors->add( 'twitter_name_error', __( '<strong>ERROR</strong>: Please use letters, numbers, spaces and underscores only.', 'sage' ) );
	}
	if ( empty( $_POST['terms'] ) ) {
		$errors->add( 'terms_error', __( '<strong>ERROR</strong>: You must agree to the terms.', 'sage' ) );
	}
	return $errors;
}
add_filter( 'registration_errors', 'tutsplus_validate_registration', 10, 3 );

上面的过滤器传递了三个参数:

  1. 已处理的错误
  2. 用户的电子邮件
  3. 用户的清理用户名

在提交表单之后但数据到达数据库之前触发该函数。 在我们的案例中,我们正在检查字段是否为空,以及我们的Twitter名称输入中是否有任何奇怪的字符。 如果其中任何一个为真,我们会将错误消息传递给返回的$error对象。

注意:如果您得到一个空的错误框,请不要担心。 我们在第一部分中安装的安全性插件具有隐藏消息的默认设置。 在“ WordPress调整”部分下,取消选中“ 禁用登录错误消息 ”选项。

登录页面显示四个错误消息

现在,我们的难题的最后一步是:处理数据,以便针对该用户将其保存在我们的数据库中。 再次,我们将使用WordPress定义的动作( user_register )挂接到该过程。 它需要一个参数,即user_id它知道-即谁的方式来保存对数据。 假设$_POST 超全局包含我们的数据,我们可以使用update_user_meta保存它。

<?php

/**
 * Process the additional fields.
 *
 * @param user_id
 */
function tutsplus_process_registration( $user_id ) {
    if ( ! empty( $_POST['twitter_name'] ) ) {
		update_user_meta( $user_id, 'twitter_name', trim( $_POST['twitter_name'] ) );
	}
	if ( ! empty( $_POST['terms'] ) ) {
		update_user_meta( $user_id, 'terms', trim( $_POST['terms'] ) );
	}
}
add_action( 'user_register', 'tutsplus_process_registration' );

管理员中的自定义字段

现在,我们已经收集了用户的数据-包括我们的自定义字段-但我们无法在WordPress管理员中编辑这些值。 让我们把它挂起来。 在我们的admin.php中添加以下功能:

<?php
/** Display in the wp backend
 * http://codex.wordpress.org/Plugin_API/Action_Reference/show_user_profile
 *
 * Show custom user profile fields
 * @param  obj $user The WP user object.
 * @return void
 */
function tutsplus_custom_user_profile_fields( $user ) {
?>
<table class="form-table">
    <tr>
		<th>
			<label for="twitter_name"><?php __e( 'Twitter name','sage' ); ?></label>
		</th>
		<td>
			<input type="text" name="twitter_name" id="twitter_name" value="<?php echo esc_attr( get_the_author_meta( 'twitter_name', $user->ID ) ); ?>" class="regular-text" />
		</td>
	</tr>
</table>
<?php
}
// Hooks near the bottom of profile page (if current user)
add_action('show_user_profile', 'tutsplus_custom_user_profile_fields');
// Hooks near the bottom of the profile page (if not current user)
add_action('edit_user_profile', 'tutsplus_custom_user_profile_fields');

使用一些WordPress操作,我们可以轻松添加自定义字段。




现在处理自定义用户元。

<?php
/** Update the custom meta
 * https://codex.wordpress.org/Plugin_API/Action_Reference/personal_options_update
 * https://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile_update
 *
 * Show custom user profile fields
 * @param  int user_id.
 */
function tutsplus_update_extra_profile_fields( $user_id ) {

   if ( current_user_can( 'edit_user', $user_id ) )

      update_user_meta( $user_id, 'twitter_name', $_POST['twitter_name'] );
}
// Hook is used to save custom fields that have been added to the WordPress profile page (if current user)
add_action( 'personal_options_update', 'tutsplus_update_extra_profile_fields' );

// Hook is used to save custom fields that have been added to the WordPress profile page (if not current user)
add_action( 'edit_user_profile_update', 'tutsplus_update_extra_profile_fields' );

登录时重定向

最后,我们希望在用户登录时将其重定向到特定页面。默认情况下,会将其发送到WordPress后端。 算了 让我们将它们发送到“我的帐户”页面。 因此,首先,您需要在后端创建此页面。 现在不必担心内容,因为我们将在本系列的下一部分中介绍它。

现在我们有了页面,将下面的代码粘贴到____。php中并对其进行测试。 假设您的“我的帐户”页面上有my-account那么它应该可以正常工作。 再一次,我们使用了WordPress过滤器来实现这种魔术效果。

简而言之,代码将检查用户,如果他们是管理员用户,则将他们定向到默认位置,否则将重定向到“ my-account页面。 当用户使用过滤器login_redirect登录时,将触发所有这些操作。

<?php

/**
 * Redirect user after successful login.
 *
 * @param string $redirect_to URL to redirect to.
 * @param string $request URL the user is coming from.
 * @param object $user Logged user's data.
 * @return string
 */
function tutsplus_redirect_on_login( $redirect_to, $request, $user ) {
    //is there a user to check?
	global $user;
	if ( isset( $user->roles ) && is_array( $user->roles ) ) {
		//check for admins
		if ( in_array( 'administrator', $user->roles ) ) {
			// redirect them to the default place
			return $redirect_to;
		} else {
			return home_url('profile');
		}
	} else {
		return $redirect_to;
	}
}
add_filter( 'login_redirect', 'tutsplus_redirect_on_login', 10, 3 );

外发电子邮件快速说明

用户在您的网站上注册后,他们将收到确认电子邮件。 同样,如果用户忘记了密码,他们也可以通过电子邮件检索密码。 因此,重要的是,如果我们希望成为会员网站,那么请给予这些电子邮件一定的关注和关注。

现在有几种方法可以做到这一点。 对于那里的所有纯粹主义者,您可以使用WordPress过滤器来更改电子邮件内容类型并设置电子邮件样式-请参阅WordPress Codex 。 另外,也有许多为此目的而设计的插件。

我个人喜欢使用MailChimp的Mandrill从WordPress网站发送电子邮件。 设置起来不是很棘手,它具有很多功能,其中之一就是能够将模板/样式应用于外发电子邮件。

下一步是什么?

在本系列的下一个也是最后一部分,我们将创建一个基本帐户部分,登录的用户将可以编辑其详细信息。 我们还希望管理员能够从WP admin区域编辑这些详细信息,因此我们也将在其中添加一些自定义字段。

如果您有任何建议或问题,请发表评论。 我会尽力及时答复。

注意事项

请注意:如果您要从GitHub存储库下载代码,它将包含所有文件以启动您的主题并开始运行。 这个想法是,您可以获取存储库,然后运行必要的Gulp和Bower命令 ,您就可以离开! 如果只需要包含特定于该系列代码的文件,则下面列出这些文件。

翻译自: https://code.tutsplus.com/tutorials/how-to-develop-a-membership-site-with-wordpress-part-2--cms-23389

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值