Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AndreaBarghigiani/be27a1ec81a46f9c0c9956b27ea8ffa9 to your computer and use it in GitHub Desktop.
Save AndreaBarghigiani/be27a1ec81a46f9c0c9956b27ea8ffa9 to your computer and use it in GitHub Desktop.
Articolo: "Tutto quello che Devi Sapere sui Ruoli degli Utenti WordPress" (https://skillsandmore.org/wordpress-ruoli/)
<?php
//Per Ruolo
function aggiungi_capability() {
// prendo il ruolo author
$ruolo = get_role( 'author' );
//Aggiungo la mia Capability
$role->add_cap( 'aggiungi_prodotto' );
}
add_action( 'admin_init', 'aggiungi_capability');
//Per Utente
$user = new WP_User( $user_id );
$user->add_cap( 'can_edit_posts');
<?php
//Aggiungi un Ruolo ad un Utente
$user_id = 3;
$ruolo = 'shop_manager';
$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $ruolo ) );
<?php
//Controlla la capability di un utente loggato
if( current_user_can('edit_others_posts') ){
echo "Puoi modificare gli articoli degli altri";
}
<?php
//Controlla un dato utente
if ( user_can( 6, 'edit_others_posts' ) ) {
// Fai qualcosa
}
<?php
//Crea un Ruolo Personalizzato
$aggiungi_ruolo = add_role(
'shop_manager',
__( 'Shop Manager' ),
array(
'edit_product' => true, // true ci attiva la capability
'delete_shop_coupon_terms' => true,
'read_shop_order' => false, // false la disattiva
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment