Skip to:
Content
Pages
Categories
Search
Top
Bottom

Random User when /Members is loaded?


  • coolhunt
    Participant

    @coolhunt

    Hey Guys,

    Does anybody have a quick snippet that I can stick in to bp-custom.php that randomizes the list of members when the url mydomain.com/members is loaded. I’d also like to increase the number listed to more than 20

    Any leads would be helpful
    (I got lost when i was reading the doc) LOL

Viewing 6 replies - 1 through 6 (of 6 total)

  • danbp
    Participant

    @danbp

    Hi,

    you need a child theme and a small code modification.

    The code is explained here.

    if ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) is to replace by something like (can be different)
    if ( bp_has_members( 'type=random' ) )

    In a “buddypress” folder of your child theme, you add a copy of this file:
    wp-content/plugins/buddypress/bp-templates/bp-legacy/buddypress/members/members-loop.php

    What ever you modify in that file while now be interpreted by BuddyPress. The advantage of this method is that BP can be updated while your modification in the child will always be save and taken in account over the current version.

    You can also search the forum for members-loop to get other information.


    coolhunt
    Participant

    @coolhunt

    @danbp Is it possible to this in bp-custom.php


    coolhunt
    Participant

    @coolhunt

    I found this handy

    It works in BP-custom.php — im trying to get it to load instead of “alphabetical”

    // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) ) exit;
     
    class BP_Loop_Filters {
     
        /**
         * Constructor
         */
        public function __construct() {
            $this->setup_actions();
        }
     
        /**
         * Actions
         *
         * @uses bp_is_active()
         * @uses is_multisite()
         */
        private function setup_actions() {
            /**
             * Adds the random order to the select boxes of the Members, Groups and Blogs directory pages
             */
            // Members component is core, so it will be available
            add_action( 'bp_members_directory_order_options', array( $this, 'random_order' ) );
     
            // You need to check Groups component is available
            if( bp_is_active( 'groups' ) )
                add_action( 'bp_groups_directory_order_options',  array( $this, 'random_order' ) );
     
            // You need to check WordPress config and that Blogs Component is available
            if( is_multisite() && bp_is_active( 'blogs' ) )
                add_action( 'bp_blogs_directory_order_options',   array( $this, 'random_order' ) );
        }
     
        /**
         * Displays a new option in the Members/Groups & Blogs directories
         *
         * <a class="bp-suggestions-mention" href="https://buddypress.org/members/return/" rel="nofollow">@return</a> string html output
         */
        public function random_order() {
            ?>
            <option value="random"><?php _e( 'Random', 'buddypress' ); ?></option>
            <?php
        }
     
    }
     
    // 1, 2, 3 go !
    function bp_loop_filters() {
        return new BP_Loop_Filters();
    }
     
    add_action( 'bp_include', 'bp_loop_filters' );
    

    Renato Alves
    Moderator

    @espellcaste

    You can use the ‘bp_after_has_members_parse_args’ hook to filter for random users adding it to the bp-cuscom.php file.

    function random_users( $args ) {
    
    	if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
    		return $args;
    	}
    
    	$args['type'] = 'random';
    
    	return (array) $args;
    }
    add_filter( 'bp_after_has_members_parse_args', 	'random_users', 10 );

    coolhunt
    Participant

    @coolhunt

    @espellcaste @danbp
    That worked well!!! *thank you – thank you*

    I also added the code below to display 50 users – is there a way to combine them or factor it down?

    function more_users( $args ) {
    
    	if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
    		return $args;
    	}
    
    	$args['per_page'] = '50';
    
    	return (array) $args;
    }
    add_filter( 'bp_after_has_members_parse_args', 	'more_users', 10 );
    
    ?>

    Renato Alves
    Moderator

    @espellcaste

    You can add as much options as you want:

    function more_users( $args ) {
    
    	if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
    		return $args;
    	}
    
    	$args['per_page'] = '50';
    
            $args['type'] = 'random';
    
    	return (array) $args;
    }
    add_filter( 'bp_after_has_members_parse_args', 	'more_users', 10 );
Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar