到目前为止,我已经找到了以下代码片段,用于使用如下所示的小格式更改用户角色, 我需要一个简单的表单,在提交完成后向管理员发送电子邮件,
提交完毕后,我需要重新加载同一页面或重定向到相同的URL。
我是Wordpress的新手,谢谢。
对不起,我的英语不好。
add_shortcode( 'upgrade_to_trusted_zone', 'upgrade_to_trusted_zone' );
function upgrade_to_trusted_zone() {
// Stop if user is not logged in.
if ( ! is_user_logged_in() )
return;
ob_start();
?>
<form method="post" action="">
Please select a role:<br/>
<select name="role">
<option value="contributor" selected="selected">Member</option>
<option value="author">Trusted Zoner</option>
</select>
<input name="terms" type="checkbox" required value="Yes" />
<label>I have read and agree of the <a href="">terms and conditions</a>.</label>
<input type="hidden" name="submitted" id="submitted" value="true" /><button type="submit">Upgrade</button>
</form>
<?php
// Do not process anything if it's not $_POST
if ( ! isset( $_POST['role'] ) )
return;
// Never trust user input.
$role = sanitize_key( $_POST['role'] );
if ( ! in_array( $role, array( 'contributor', 'author' ) ) )
return;
// Get the user object
$user = new WP_User( get_current_user_id() );
$index = key( $user->roles );
$user_role = $user->roles[ $index ];
// User already got that user
if ( $user_role == $role ) {
echo sprintf( __( 'You already have %s role.' ), $role );
} else {
// update user role
$user->set_role( $role );
echo sprintf( __( 'Your role was changed to %s.' ), $role );
}
$output = ob_get_contents();
ob_end_clean();
return $output;
}