因此,首先,我们需要某种提交表单。这里有一个简单的例子,很明显,它可以是你想要的任何东西。这只是这个例子。提交时向此人发送电子邮件,并在wp\\U选项表中为其设置密码密钥。
<?php
add_action( \'init\', \'wpse15535_add_shortcode\' );
function wpse15535_add_shortcode()
{
add_shortcode( \'invite-form\', \'wpse15535_invite_form\' );
}
function wpse15535_invite_form()
{
?>
<form action="" method="post">
<input type="hidden" name="wpse15535_action" value="send_invite" />
<input type="text" name="wpse15535_email" />
<input type="submit" value="<?php _e( \'Send Invite\' ); ?>" />
</form>
<?php
// Do some stuff if our action is set.
if( isset( $_POST[\'wpse15535_action\'] ) && \'send_invite\' == $_POST[\'wpse15535_action\'] )
{
// Get the email
$email = isset( $_POST[\'wpse15535_email\'] ) && is_email( $_POST[\'wpse15535_email\'] ) ? $_POST[\'wpse15535_email\'] : false;
if( ! $email ) return; // bad email? bail.
// generate a random 30 character string.
$key = wp_generate_password( 30 );
// store our keys in the options table
$opts = get_option( \'wpse15535_keys\' );
$opts = (array) $opts;
$opts[$email] = $key;
update_option( \'wpse15535_keys\', $opts );
// Send an email!
$message = "You\'re invited to join a sweet community! Click here: " . home_url( \'wp-login.php?action=register&invite_key=\' . $key );
wp_mail( $email, "You\'re invited!", $message );
}
}
然后写在登记表上。确保在中选中“任何人都可以注册”
settigns > general
选项。
发送给此人的电子邮件包含一个url,其url参数为invite键invite_key
. 因此,首先,我们将挂接到注册表中,并用键(如果存在)回显隐藏的输入。
<?php
add_action( \'register_form\', \'wpse15535_add_key\' );
function wpse15535_add_key()
{
if( isset( $_GET[\'invite_key\'] ) )
{
echo \'<input type="hidden" name="invite_key" value="\' . esc_attr( $_GET[\'invite_key\'] ) . \'" />\';
}
}
接下来,我们将连接到
registration_errors
滤器我们需要看看是否已经有任何错误,如果有,就保释。然后我们看看是否有钥匙。如果没有,请添加错误和消息。我们得到包含are email=>密钥对的选项,并查看输入的电子邮件是否与我们的密钥匹配。如果不是,则添加并出错并返回。
如果没有问题,用户应该能够注册没有问题。
<?php
add_filter( \'registration_errors\', \'wpse15535_register_post\', 10, 3 );
function wpse15535_register_post( $errors, $login, $email )
{
// bail if something has gone wrong already as they won\'t be able to register.
if( $errors->get_error_codes() ) return $errors;
// If our key isn\'t set an error and bail
if( ! isset( $_REQUEST[\'invite_key\'] ) )
{
$errors->add( \'no_key\', __( \'You need an invite code to register for this site\' ) );
return $errors;
}
$opts = get_option( \'wpse15535_keys\' );
// see if this email has a key and if it matches what was submitted.
if( ! isset( $opts[$email] ) || $_REQUEST[\'invite_key\'] != $opts[$email] )
{
$errors->add( \'invalid_key\', __( \'Invalid Registration Key\' ) );
return $errors;
}
// everything okay? Just return the errors and let it go through.
return $errors;
}
作为插件:
https://gist.github.com/1181933