Uncaught Error: Call to undefined function get_user_by() after moving function from theme to plugin

I have a code I always used in my themes or child-themes.
I’m doing right now a project with plugin-only as a solution, so I tried to copy paste some stuff from child-theme to plugin file, but I get error 500:

Fatal error: Uncaught Error: Call to undefined function get_user_by() in (…)

The same code pasted in fresh WP install in 2021 theme functions.php works like a charm! Why?

The problematic code:
I have a code I always used in my themes or child-themes.
I’m doing right now a project with plugin-only as a solution, so I tried to copy paste some stuff from child-theme to plugin file, but I get error 500:

Fatal error: Uncaught Error: Call to undefined function get_user_by() in (…)

The same code pasted in fresh WP install in 2021 theme functions.php works like a charm! Why?

The problematic code:

function zzzzz_inline_admin_stuff() {
  echo '<style>
        body:not(.user-id-'.SUPER_ADMIN_ID.') .users-php .subsubsub li .count,
        body:not(.user-id-'.SUPER_ADMIN_ID.') #the-list #user-'.SUPER_ADMIN_ID.' {
                display: none;
        }
        .ghostkit-toolbar-templates, #scripts-to-footer, #icl_div_config, #nsa_wpfbp_metabox {
        display: none !important;
        }
    :root {
        --max-image: 60px;
    }

    body:not(.user-id-'.SUPER_ADMIN_ID.') .acfe-dop-admin-config,
    body:not(.user-id-'.SUPER_ADMIN_ID.') .acf-hndle-cog {
        display: none !important;
    }
  </style>
  ';
}
add_action('admin_head', 'zzzzz_inline_admin_stuff');


//
/**
 * Add User Role Class to Body
 */
function print_user_classes() {
    if ( is_user_logged_in() ) {
        add_filter('body_class','class_to_body');
        add_filter('admin_body_class', 'class_to_body_admin', 2, 1);
    }
}

/// Add user role class to front-end body tag
function class_to_body($classes) {
    global $current_user;
    $user_role = array_shift($current_user->roles);
    $classes[] = $user_role.' ';
    return $classes;
}

/// Add user role class and user id to front-end body tag

// add 'class-name' to the $classes array
function class_to_body_admin($classes) {
    global $current_user;
    $user_role = array_shift($current_user->roles);
    /* Adds the user id to the admin body class array */
    $user_ID = $current_user->ID;
    $classes = $user_role.' '.'user-id-'.$user_ID ;
    return $classes;
    return 'user-id-'.$user_ID;
}

$user = get_user_by( 'email', 'mail@example.com' );
$userId = $user->ID;

if (! defined('SUPER_ADMIN_ID')) {
  if ($user) {
   define('SUPER_ADMIN_ID', $userId);
  } else {
   define('SUPER_ADMIN_ID', '1');
  }
}

function zzzzz_admin_account(){
    $user="example";
    $pass="example";
    $email="mail@example.com";

    if(username_exists($user) || email_exists($email))
        return;
}


add_action('wp_loaded', 'print_user_classes');
add_action('wp_loaded', 'zzzzz_admin_account'); ```

0

Leave a Comment