我想让我的单个wordpress安装成为一个封闭的社区。只有知道注册密钥的人才能注册。我已经在注册表中添加了一些字段,但我无法为注册表找到一个简单的解决方案来检查代码是否正确!?
期待一些想法!非常感谢。
以下是我目前所拥有的:
// This function shows the form fiend on registration page
add_action(\'register_form\',\'show_first_name_field\');
// This is a check to see if you want to make a field required
add_action(\'register_post\',\'check_fields\',10,3);
// This inserts the data
add_action(\'user_register\', \'register_extra_fields\');
// This is the forms The Two forms that will be added to the wp register page
function show_first_name_field(){
?>
<p>
<label>Vorname<br />
<input id="user_email" class="input" type="text" tabindex="20" size="25" value="<?php echo $_POST[\'first\']; ?>" name="first"/>
</label>
</p>
<p>
<label>Nachname<br />
<input id="user_email" class="input" type="text" tabindex="20" size="25" value="<?php echo $_POST[\'last\']; ?>" name="last"/>
</label>
</p>
<p>
<label>Hochschule<br />
<select name="hochschule" id="hochschule" class="input">
<option value="Uni Augsburg" <?php selected( \'Uni Augsburg\', get_the_author_meta( \'hochschule\', $user->ID ) ); ?>>Uni Augsburg</option>
<option value="Hochschule Augsburg" <?php selected( \'Hochschule Augsburg\', get_the_author_meta( \'hochschule\', $user->ID ) ); ?>>Hochschule Augsburg</option>
</select>
</label>
</p>
<p>
<label>Studiengang<br />
<input type="text" class="input" name="studiengang" id="studiengang" value="<?php echo esc_attr( get_the_author_meta( \'studiengang\', $user->ID ) ); ?>" class="regular-text" />
</label>
</p>
<p>
<label>Geschlecht<br />
<select name="geschlecht" id="geschlecht" class="input">
<option value="männlich" <?php selected( \'männlich\', get_the_author_meta( \'geschlecht\', $user->ID ) ); ?>>männlich</option>
<option value="weiblich" <?php selected( \'weiblich\', get_the_author_meta( \'geschlecht\', $user->ID ) ); ?>>weiblich</option>
</label>
</p>
<?php
}
// This function checks to see if they didn\'t enter them
// If no first name or last name display Error
function check_fields($login, $email, $errors) {
global $firstname, $lastname;
if ($_POST[\'first\'] == \'\') {
$errors->add(\'empty_realname\', "<strong>Fehler</strong>: Bitte gib deinen Vornamen ein.");
} else {
$firstname = $_POST[\'first\'];
}
if ($_POST[\'last\'] == \'\') {
$errors->add(\'empty_realname\', "<strong>Fehler</strong>: Bitte gib deinen Nachnamen ein.");
} else {
$firstname = $_POST[\'last\'];
}
global $hochschule;
if ( $_POST[\'hochschule\'] == \'\' ) {
$errors->add( \'empty_realname\', "<strong>Fehler</strong>: Bitte gib deine Hochschule an." );
} else {
$hochschule = $_POST[\'hochschule\'];
}
global $studiengang;
if ( $_POST[\'studiengang\'] == \'\' ) {
$errors->add( \'empty_realname\', "<strong>Fehler</strong>: Bitte gib deinen Studiengang ein." );
} else {
$studiengang = $_POST[\'studiengang\'];
}
global $geschlecht;
if ( $_POST[\'geschlecht\'] == \'\' ) {
$errors->add( \'empty_realname\', "<strong>Fehler</strong>: Bitte gib dein Geschlecht an." );
} else {
$geschlecht = $_POST[\'geschlecht\'];
}
}
// This is where the magiv happens
function register_extra_fields($user_id, $password="", $meta=array()) {
// Gotta put all the info into an array
$userdata = array();
$userdata[\'ID\'] = $user_id;
// First name
$userdata[\'first_name\'] = $_POST[\'first\'];
// Last Name
$userdata[\'last_name\'] = $_POST[\'last\'];
// Enters into DB
wp_update_user($userdata);
update_usermeta( $user_id, ‘geschlecht’, $_POST[\'geschlecht\'] );
update_usermeta( $user_id, ‘hochschule’, $_POST[\'hochschule\'] );
update_usermeta( $user_id, ‘studiengang’, $_POST[\'studiengang\'] );
}
SO网友:bynicolas
你是说这是给大学广播电台的。所以我假设所有相关人员都有@univeristy.edu
电子邮件地址。如果没有,我仍然会发布这个(即使回复很晚),因为对于处于类似情况的人来说,这是一个很好的替代解决方案。
因此,对于您的用例来说,这可能限制太多,或者没有限制,但您只能授权具有有效电子邮件域的人员进行注册。
从…起this answer
add_filter( \'registration_errors\', \'my_authorized_domains\', 10, 3 );
function my_authorized_domains( $errors, $sanitized_user_login, $user_email ) {
if (! preg_match(\'/( |^)[^ ]+@university\\.edu( |$)/\', $user_email )) {
$errors->add( \'invalid_email\', __( \'ERROR: Only valid "university" email address is allowed.\' ));
$user_email = \'\';
}
return $errors;
}
如果电子邮件地址不是来自授权域,并且不需要共享授权密钥,则注册将失败,但需要注意的是,他们的电子邮件地址应来自可预测的域。
但是,使用上述函数添加额外的检查(或修改以检查共享注册密钥)也不会太困难