Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 480 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wordpress list user roles

#1
I am working ion a wordpress project and digging around into roles etc.

I have the following code which basically gets all available roles:

<?php
global $wp_roles;
$roles = $wp_roles->get_names();

// Below code will print the all list of roles.
print_r($roles);
?>


when i run the above code i get the following output:

array ( [administrator] => Administrator [editor] => Editor [author] => Author [contributor] => Contributor [subscriber] => Subscriber [basic_contributor] => Basic Contributor )

i would like the above to be stripped from the array and into just an unordered list. How would i achieve this?

Thanks Dan
Reply

#2
You can use a [foreach][1] loop, to loop through each of the roles in the array.

<ul>
<?php foreach($roles as $role) { ?>
<li><?php echo $role;?></li>
<?php }//end foreach ?>
</ul>


[1]:

[To see links please register here]

Reply

#3
**Here is the code to make dropdown of wordpress user role**

<?php global $wp_roles; ?>

<select name="role">
<?php foreach ( $wp_roles->roles as $key=>$value ): ?>
<option value="<?php echo $key; ?>"><?php echo $value['name']; ?></option>
<?php endforeach; ?>
</select>
Reply

#4
Just an additional information. There's also a function [wp_dropdown_roles()][1] which gives you the roles as option html elements.

<select>
<?php wp_dropdown_roles(); ?>
</select>

You can also set the default selected value by passing the role slug as a parameter.

<select>
<?php wp_dropdown_roles( 'editor' ); ?>
</select>


[1]:

[To see links please register here]

Reply

#5
Since the [l10n functions do not accept variables][1], [`translate_user_role()`][2] is required to translate the role names properly. Also, using [`wp_roles()`][3] rather than the global variable `$wp_roles` is the safer approach, for it first checks if the global is set and if not will set it and return it.

$roles = wp_roles()->get_names();

foreach( $roles as $role ) {
echo translate_user_role( $role );
}


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#6
you would like to display a select list of available WordPress role names.

$roles_obj = new WP_Roles();
$roles_names_array = $roles_obj->get_names();
echo '<select name="role">';
foreach ($roles_names_array as $role_name) {
echo '<option>'.$role_name.'</option>';
}
echo '</select>';
Reply

#7
If you are looking for Dropdown options, use WordPress native function `wp_dropdown_roles` This will give Translated user roles

Here is the code example

<select name="default_role" id="default_role">
<?php wp_dropdown_roles( get_option( 'default_role' ) ); ?>
</select>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through