From time to time i want to give my registered users the chance to upgrade their user role by themselves for free. I tried this by writing a little plugin, but sady i can’t get the html form to show when using the shortcode, and therefore cant test if it works or not. I would be really thankful for any assistance.
function upgrade_to_premium() {
if( is_user_logged_in() ) {
if( is_page( 'upgrade-to-premium' ) ) {
$current_user = wp_get_current_user();
if( $current_user->roles[0] == "subscriber" || $current_user->roles[0] == "premium" ) {
$user_id = $current_user->id;
$role = $current_user->roles[0];
if( $_POST['role']){
if( $_POST['role'] == $role ) {
echo "Sorry, you are already a " . $role . "!";
} else {
$role = $_POST['role'];
$userdata = array();
$userdata['ID'] = $user_id;
$userdata['role'] = $role;
wp_update_user($userdata);
echo "Your user type has been changed! You are now a " . $role . "!";
}
}
?>
<form method="post" action="">
Please select a role:<br/>
<select name="role">
<option value="subscriber" selected="selected">Subscriber</option>
<option value="premium">Premium</option>
</select>
<INPUT TYPE="submit" name="submit" />
</form>
<?php
}
}
}
}
add_shortcode( 'upgrade_to_premium', 'upgrade_to_premium' );