Display Filtered profile by role

I’m modifying a wordpress theme that has a filter page of user profile.
The whole filter mechanism seems to be based on slugs.

I would like to add a filter by role, but roles don’t have any slug.

add_action( ‘register_form’, ‘role_register_form’ );
function list_role() {

global $wp_roles;
echo ‘<select name=”list_all_roles” class=”fre-chosen-single”>’;
echo ‘<option disabled selected value> — </option>’;
foreach ( $wp_roles->roles as $key=>$value ):
echo ‘<option value=”‘.$key.'” name=”‘.$value->slug.'”>’.$value[‘name’].'</option>’;
endforeach;
echo ‘</select>’;
}

The display is good but the filter is not working since roles don’t have any slugs.

Here is the javascript code of the filter mechanism

// filter skill
if (view.getUrlParameter(‘role_profile’)) {
setTimeout(function () {
$(‘list_all_roles li a[name=”‘ + view.getUrlParameter(‘role_profile’) + ‘”]’).click();
$(‘body’).click();
}, 1000);
}
$(‘.list_all_roles’).on(‘click’, function (event) {
var $target = $(event.currentTarget),
name = $target.attr(‘data-name’),
selected = $target.find(‘li a.active’);
if (name !== ‘undefined’) {
role = _.map(selected, function (element) {
//return $(element).html(); 1.8.4
return $(element).attr(‘name’); // 1.8.6 filter in prfiles page.
});
view.query[name] = role;
view.page = 1;
view.fetch($target);
}
});

and here the getUrlParameter function :

getUrlParameter: function (name) {
var results = new RegExp(‘[?&]’ + name + ‘=([^&#]*)’).exec(window.location.href);
if (results == null) {
return null;
}
else {
return results[1] || 0;
}
}

Read more here:: Display Filtered profile by role

Leave a Reply

Your email address will not be published. Required fields are marked *