Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
can you use a function to save lines during an ajax call?

Apologies if I didn't quite word my title correctly. I am currently finishing up this Wordpress AJAX challenge in which we needed to display particular user-types to the screen depending on a plugin's selection. The function below is whats being called in my AJAX call. I currently am running multiple instances of this code below, one for each user-type:

$user_roles = get_option( 'show_users' );
$count = 0;

if($user_roles['administrator']) {

    $users = get_users( array (
        'role' => 'administrator'
    ));

    foreach ($users as $user) {
        $result[$count] = $user->display_name;
        $count++;
    }

}

However, I thought I might be able to create a function that can save me some code (and maybe even use a foreach later to save even more), but the code does not seem to work. Function I tried using (followed by calling the function) :

function save_time ($x) {
    if ($user_roles[$x]) {
        $users = get_users( array (
            'role' => $x
        ));

        foreach ($users as $user) {
            $result[$count] = $user->display_name;
            $count++;
        }
    }
}

save_time('administrator');

Is there something I can do make this work? Am I forgetting how functions work in general? Thank you!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You already have an array of roles so you could just use that.

foreach($user_roles as $role => $values) {
    $users = get_users( array (
        'role' => $role
    ));
    foreach ($users as $user) {
        $result[$count] = $user->display_name;
        $count++;
    }
}

Then you don't even have to define one for each type.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda