0

I'm running a wordpress plugin and continue to receive errors like:

Undefined variable $admin in C:\xampp\htdocs\wordpress\apps\wordpress\htdocs\wp-content\plugins\wordpress-database\vragen_plugin.php on line 791

This was my code:

    // If ur an admin, you have access to the following page
    if($admin == '1'){
// When going to /Stel-vraag?createquestion=...
        if (isset($_POST['createquestion'])) {
            $short_question = filter_input(INPUT_POST, 'short_question', FILTER_SANITIZE_SPECIAL_CHARS);
            $long_question = filter_input(INPUT_POST, 'long_question', FILTER_SANITIZE_SPECIAL_CHARS);
            if (!isset($_POST['comment'])) { $comment = 'Nee';} else { $comment = filter_input(INPUT_POST, 'comment', FILTER_SANITIZE_SPECIAL_CHARS); }
            $uniq_name = substr(md5(uniqid(rand())),0,10);
    
1
  • 3
    So where did you define $admin? It's not defined, so the error happens. Perhaps you meant to use is_admin() ? Commented Dec 1, 2022 at 1:45

1 Answer 1

0

The error is telling that when you say:

if($admin == '1'){

PHP has no idea what you are talking about. There is no $admin so it cannot ever equal 1.

As Rick Hellewell mentioned in the comments, you might actually want is_admin() which is a function that returns a boolean (true or false).

It is quite likely the code you actually want would be like this:

// If ur an admin, you have access to the following page
if( is_user_admin() ){
// When going ...[snip]...

Edit: I originally suggested is_admin() which I was reminded checks the page is loaded in the admin area. I should have said is_user_admin()

3
  • Please be aware that is_admin DOESN'T return if the user is an admin or not, but instead returns true if you are viewing a page within the "admin" (Backend) section of Wordpress at the moment this function is run... Commented Dec 2, 2022 at 12:20
  • Ah, yeah. You are right. HU is Sebastian. I shall update my answer. Commented Dec 4, 2022 at 8:19
  • This is still not what you want ;) is_user_admin returns true if you are viewing the user admin screen in the backend, i.e. /wp-admin/user/ . If you want to know if the current user is in the admin role, you should use current_user_can('administrator') if you want to target the administrator role specifically, or current_user_can('manage_options') if you want to target users that have administrative rights... Commented Dec 5, 2022 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.