This topic has 3 replies, 2 voices, and was last updated 9 years by bazaarocommunity.

  • Author
  • #54008
     bazaarocommunity
    Participant

    I’ve tried to follow the BuddyPress Codex for creating “featured” groups, but for some reason it is not working, any ideas? The featured group checkbox appears in the group settings, but on the group index page selecting order by featured does not actually change which groups are displayed. This is the code I placed into my Kleo-child theme functions.php.

    //it’s important to check the BP_Group_Extension is available
    if( class_exists( ‘BP_Group_Extension’ ) ) :
    /**
    * This is a quick and dirty class to illustrate “bpgmq”
    * bpgmq stands for BuddyPress Group Meta Query…
    * The goal is to store a groupmeta in order to let the community administrator
    * feature a group.
    * Featured groups will be filterable from the groups directory thanks to a new option
    * and to a filter applied on bp_ajax_query_string()
    *
    * This class is an example, it would be much better to use the group extension API
    */
    class bpgmq_feature_group {

    public function __construct() {
    $this->setup_hooks();
    }

    private function setup_hooks() {
    // in Group Admin UI, you add a new metabox to display a checkbox to featured the displayed group
    add_action( ‘bp_groups_admin_meta_boxes’, array( $this, ‘admin_ui_edit_featured’ ) );
    // Once the group is saved you store a groupmeta in db, the one you will search for in your group meta query
    add_action( ‘bp_group_admin_edit_after’, array( $this, ‘admin_ui_save_featured’), 10, 1 );

    /* The groups loop uses bp_ajax_querystring( ‘groups’ ) to filter the groups
    depending on the selected option */
    add_filter( ‘bp_ajax_querystring’, array( $this, ‘filter_ajax_querystring’ ), 20, 2 );

    /* finally you create your options in the different select boxes */
    // you need to do it for the group directory
    add_action( ‘bp_groups_directory_order_options’, array( $this, ‘featured_option’ ) );
    // and for the groups tab of the user’s profile
    add_action( ‘bp_member_group_order_options’, array( $this, ‘featured_option’ ) );
    }

    /**
    * registers a new metabox in Group Admin UI, edit group panel
    * @uses add_meta_box() to register our meta box
    */
    public function admin_ui_edit_featured() {
    add_meta_box(
    ‘bpgmq_feature_group_mb’,
    __( ‘Featured Group’ ),
    array( &$this, ‘admin_ui_metabox_featured’),
    get_current_screen()->id,
    ‘side’,
    ‘core’
    );
    }

    /**
    * Displays the meta box
    * @param BP_Groups_Group $item the group being edited
    * @uses groups_get_groupmeta() to get the featured attribute of the group
    * @uses checked() to eventually add a checked attribute if the group is featured
    * @uses wp_nonce_field() for security reasons
    */
    public function admin_ui_metabox_featured( $item = false ) {
    if( empty( $item ) )
    return;

    // Using groups_get_groupmeta to check if the group is featured
    $is_featured = groups_get_groupmeta( $item->id, ‘_bpgmq_featured_group’ );
    ?>
    <p>
    <input type=”checkbox” id=”bpgmq-featured-cb” name=”bpgmq-featured-cb” value=”1″ <?php checked( 1, $is_featured );?>> <?php _e( ‘Mark this group as featured’ );?>
    </p>
    <?php
    wp_nonce_field( ‘bpgmq_featured_save_’ . $item->id, ‘bpgmq_featured_admin’ );
    }

    function admin_ui_save_featured( $group_id = 0 ) {
    if ( ‘POST’ !== strtoupper( $_SERVER[‘REQUEST_METHOD’] ) || empty( $group_id ) )
    return false;

    check_admin_referer( ‘bpgmq_featured_save_’ . $group_id, ‘bpgmq_featured_admin’ );

    // You need to check if the group was featured so that you can eventually delete the group meta
    $was_featured = groups_get_groupmeta( $group_id, ‘_bpgmq_featured_group’ );
    $to_feature = !empty( $_POST[‘bpgmq-featured-cb’] ) ? true : false;

    if( !empty( $to_feature ) && empty( $was_featured ) )
    groups_update_groupmeta( $group_id, ‘_bpgmq_featured_group’, 1 );
    if( empty( $to_feature ) && !empty( $was_featured ) )
    groups_delete_groupmeta( $group_id, ‘_bpgmq_featured_group’ );
    }

    public function filter_ajax_querystring( $querystring = ”, $object = ” ) {

    /* bp_ajax_querystring is also used by other components, so you need
    to check the object is groups, else simply return the querystring and stop the process */
    if( $object != ‘groups’ )
    return $querystring;

    // Let’s rebuild the querystring as an array to ease the job
    $defaults = array(
    ‘type’ => ‘active’,
    ‘action’ => ‘active’,
    ‘scope’ => ‘all’,
    ‘page’ => 1,
    ‘user_id’ => 0,
    ‘search_terms’ => ”,
    ‘exclude’ => false,
    );

    $bpgmq_querystring = wp_parse_args( $querystring, $defaults );

    /* if your featured option has not been requested
    simply return the querystring to stop the process
    */
    if( $bpgmq_querystring[‘type’] != ‘featured’ )
    return $querystring;

    /* this is your meta_query */
    $bpgmq_querystring[‘meta_query’] = array(
    array(
    ‘key’ => ‘_bpgmq_featured_group’,
    ‘value’ => 1,
    ‘type’ => ‘numeric’,
    ‘compare’ => ‘=’
    )
    );

    // using a filter will help other plugins to eventually extend this feature
    return apply_filters( ‘bpgmq_filter_ajax_querystring’, $bpgmq_querystring, $querystring );
    }

    public function featured_option() {
    ?>
    <option value=”featured”><?php _e( ‘Featured’ ); ?></option>
    <?php
    }

    }

    /**
    * Let’s launch !
    *
    * Using bp_is_active() in this case is not needed
    * But i think it’s a good practice to use this kind of check
    * just in case 🙂
    *
    * @uses bp_is_active([component]) to check the group component is active
    */
    function bpgmq_feature_group() {
    if( bp_is_active( ‘groups’) )
    return new BPGMQ_Feature_Group();
    }

    add_action( ‘bp_init’, ‘bpgmq_feature_group’ );

    endif;

    #54291
     sharmstr
    Moderator

    The code was written in 2013. If you can prove that it actually works with the latest version of BP then I’ll have a look as to why it doesnt work with kleo. In other words, prove its a kleo issue.

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution

    This support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com

    #54292
     sharmstr
    Moderator

    Actually I just added it to my site and it did put the featured groups first in the display. So it works with Kleo.

    Not sure why it wont work on your site. Contact the person who wrote the code.

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution

    This support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com

    #54728
     bazaarocommunity
    Participant

    I got it to work. I think I must have had typos.

Viewing 4 posts - 1 through 4 (of 4 total)

The forum ‘KLEO’ is closed to new topics and replies.

Log in with your credentials

Forgot your details?