Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!