Check if user is administrator - WORDPRESS php

Asked

Viewed 254 times

0

/* CHECK LOGIN ON ------------------------*/

    global $current_user;

    $current_user = wp_get_current_user();
    $user_info = get_userdata($current_user->ID);

    $first_name = $user_info->first_name;
    $user_email = $user_info->user_email;
    $meta_value = $user_info->$meta_value;      




    if (in_array("administrator", $meta_value)) {

echo "é administrador"; } else { 

        echo "não é administrador";

     } ?>

but I don’t know where the mistake is.

2 answers

0

Gabriel’s answer is correct. I just wanted to make a comment (but I don’t have enough points for it):

As this is a forum for developers, it makes sense to put the link and explain the function, even so that they can use with other roles (and capabilities).

Job documentation: https://codex.wordpress.org/Function_Reference/current_user_can

Method of use of the function:

<?php current_user_can( $capability , $object_id ); ?> 

Where $capability may be the role (function it performs - Administrator, Subscriber) or a Capability (function it performs - publish_posts, activate_plugins). And the $object_id is optional and only recommended when checking meta capabilities.

Examples:

if( current_user_can('administrator') ) {
    //ações a tomar se usuário é adiministrador
}

$ativador_de_plugin = current_user_can('activate_plugins');

0

If you want to check if the current user is adding, you can use the function current_user_can('administrator') who will return true if the user is an attachor.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.