Skip to content

Instantly share code, notes, and snippets.

@mauriciogofas
Last active August 29, 2015 14:14
Show Gist options
  • Save mauriciogofas/f5ed572338c0de40f5f9 to your computer and use it in GitHub Desktop.
Save mauriciogofas/f5ed572338c0de40f5f9 to your computer and use it in GitHub Desktop.
Banir wp - Impedir usuário de editar perfil
<?php
// Banir usuário
// Crédito: https://gist.github.com/Giuseppe-Mazzapica/11070063
add_action( 'personal_options', 'rpa_profile_ban_field' );
add_action( 'edit_user_profile_update', 'rpa_profile_ban_field_save' );
function rpa_profile_ban_field( \WP_User $user ) {
$current = wp_get_current_user();
if ( ! is_admin() || $user->ID === $current->ID ) return;
if ( ! user_can( $current, 'edit_users' ) ) return;
$target = new WP_User( $user->ID );
if ( $target->exists() && ! user_can( $target, 'edit_users' ) ) {
$banned = (int) get_user_meta( $user->ID, '_profile_banned', TRUE );
?>
<table class="form-table"><tbody><tr>
<th scope="row">Impedir edição do perfil</th><td>
<input<?php checked(1, $banned); ?> name="_profile_banned" value="1" type="checkbox">
Impedir esse usuário de editar o seu perfil?
</td></tr></tbody></table>
<?php
}
}
function rpa_profile_ban_field_save( $userid ) {
$current = wp_get_current_user();
if ( ! is_admin() || $user->ID === $current->ID ) return;
if ( ! user_can( $current, 'edit_users' ) ) return;
$target = new WP_User( $userid );
if ( ! $target->exists() || user_can( $target, 'edit_users' ) ) return;
$ban = filter_input( INPUT_POST, '_profile_banned', FILTER_SANITIZE_NUMBER_INT );
if ( (int) $ban > 0 ) {
update_user_meta( $userid, '_profile_banned', 1 );
} elseif ( get_user_meta( $userid, '_profile_banned', TRUE ) ) {
delete_user_meta( $userid, '_profile_banned' );
}
}
// Fim banir usuário
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment