bhu24
bhu24

Reputation: 41

How to Unserialize this data in wordpress

CURRENT ISSUE: I am making one plugin for listing users with there user role in WP_LIST_TABLE table. This is my query

    $this->items = $wpdb->get_results($wpdb->prepare("SELECT {$wpdb->users}.*, {$wpdb->usermeta}.meta_value as roles FROM {$wpdb->users} 
 LEFT JOIN {$wpdb->usermeta} ON {$wpdb->users}.ID = {$wpdb- >usermeta}.user_id
 WHERE {$wpdb->usermeta}.meta_key = '{$wpdb->get_blog_prefix()}capabilities'
 ORDER BY {$wpdb->users}.display_name", $per_page, $paged), ARRAY_A);

It display out like This

[roles] => a:1:{s:13:"administrator";b:1;}

How to Unserialize this data. and I want to display the First name too using this query please help me

Upvotes: 1

Views: 748

Answers (2)

bhu24
bhu24

Reputation: 41

Finally this helps me.

$input = unserialize($item['roles']);
$result = array();
foreach($input as $key => $value){
$result[] = $key;
}
$userRole = implode(",", $result);
    return $userRole;

Upvotes: 2

Komal R
Komal R

Reputation: 427

Try below code:

foreach($this->items as $value){
 echo $value->COLUMN_NAME ."<br>";
}

Please change the COLUMN_NAME to whichever column you want to display. If there's a column named e_name, then write "$value->e_name".
Its tried and tested. It works for me. Let me know if it works for you!

Upvotes: 0

Related Questions