admin管理员组

文章数量:1191726

I've been asked to help out a friend on a site he's cobbled together. I'm unfamiliar with Buddypress but on the members list page it's showing the site admin users, he'd like to exclude them but there are no plugin options to hide them.

I've had this Working to exclude one of them, but I now need to modify it to accept multiple user IDs.

Here's the current code.

add_action( 'bp_ajax_querystring', 'user_remove_mem_directory', 20, 2 );
function user_remove_mem_directory( $query, $object ) {
    if ( 'members' != $object ){
        return $query;
    }

    $excluded_user = '93'; // admin id to remove from the member directory

    $args = wp_parse_args( $query );

    if ( ! empty( $args['exclude'] ) ) {
        $args['exclude'] = $args['exclude'] . ',' . $excluded_user;
    } else {
        $args['exclude'] = $excluded_user;
    }

    $query = build_query( $args );

    return $query;
}

Any help would be much appreciated.

I've been asked to help out a friend on a site he's cobbled together. I'm unfamiliar with Buddypress but on the members list page it's showing the site admin users, he'd like to exclude them but there are no plugin options to hide them.

I've had this Working to exclude one of them, but I now need to modify it to accept multiple user IDs.

Here's the current code.

add_action( 'bp_ajax_querystring', 'user_remove_mem_directory', 20, 2 );
function user_remove_mem_directory( $query, $object ) {
    if ( 'members' != $object ){
        return $query;
    }

    $excluded_user = '93'; // admin id to remove from the member directory

    $args = wp_parse_args( $query );

    if ( ! empty( $args['exclude'] ) ) {
        $args['exclude'] = $args['exclude'] . ',' . $excluded_user;
    } else {
        $args['exclude'] = $excluded_user;
    }

    $query = build_query( $args );

    return $query;
}

Any help would be much appreciated.

Share Improve this question asked Jan 24 at 20:43 morgyfacemorgyface 3782 silver badges10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

You could make an array of ids like $ids_to_exclude = array('93',...) then loop through it using a forloop or while loop

$ids_to_exclude = array('93',...);
foreach ($ids_to_exclude as &$id) {
   function_to_call_on_each_id($id);
}

The PHP manual has really good examples and is super easy to follow. PHP Foreach

Just thought I'd post my solution, I'm thinking it's verbose, but it works. I'll gladly receive any tips on how to reduce the bloat.

I included a little foreach loop which builds a list of IDs from usernames as I feel this would make more sense to anyone looking at this in future.

<?php
// Removes specified users
add_action( 'bp_ajax_querystring', 'user_remove_mem_directory', 20, 2 );
function user_remove_mem_directory( $query, $object ) {
    if ( 'members' != $object ) {
        return $query;
    }
    $usernames = array('Admin1', 'Admin2');
    foreach ($usernames as $username) {
        $user = get_user_by('login', $username);
        $excluded_users[] = $user->ID;
    } // Build array of user IDs from login usernames
    $args = wp_parse_args( $query ); // Merges user query into array.
    if ( ! empty( $args['exclude'] ) ) {
        $args['exclude'] = $args['exclude'];
        foreach ($excluded_users as &$id) {
             $args['exclude'] .= ',' . $id; // Loop through building an array
        }
    } else {
        $args['exclude'] = $excluded_users;
    }
    $query = build_query( $args );
    return $query;
}

本文标签: phpWordPress Buddypress remove multiple users from members listStack Overflow