BuddyDev

Search

[Resolved] [Blog Categories For Groups] Auto-creating & selecting cats for groups

  • Participant
    Level: Initiated
    Posts: 2
    Hamish Erskine on #3036

    Hi there buddypressers and @sbrajesh,

    I’m new to buddypress.

    I’ve succesfully integrated creating categories after group creation using this code:

    function createCatAfterGroup() {
    global $wpdb;
    $group = $wpdb->get_row("SELECT * FROM
    wp_bp_groups ORDER BY date_created DESC");
    wp_insert_term($group->name, 'category');
    }
    add_action('groups_created_group', 'createCatAfterGroup');

    I now need to force the selection of this category for groups posts, and even hide the checkbox and section for the admin. I’ve seen a few people mention they had done it, and @sbrajesh speak about the small amount of code needed for this second step.

    What code do I need (and where) to force groups to use the category that matches their slug?
    What do I need to do to then hide category management from the admin?

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #3037

    Hi Hamish,
    Welcome to BuddyDev.
    Thank you for asking it. I had posted the code somewhere but could not find it on the forum, so posting again. Please put these codes in your bp-custom.php

    1. Let us write a function to create a category with group name and save it as bcg category for group(you won’t need the code you have used in the above post)

    
    
    /**
     * Create a Category with group name and set as BCG category
     * 
     * @param type $group
     * @return type
     */
    function bcg_custom_create_group_category( $group ) {
    		
    	$cat = wp_insert_term( $group->name, 'category' );
    
    	if ( ! is_wp_error( $cat ) ) {
    		//insert it to group meta for blog categories
    		groups_update_groupmeta( $group->id, 'group_blog_cats', array( 0 => $cat['term_id'] ) );
    		
    		return array( 0 => $cat['term_id'] );
    
    	} else {
    		//is it existing
    		$term = get_term_by( 'slug', $group->slug,  'category' );
    		if (  $term ) {
    			
    			groups_update_groupmeta( $group->id, 'group_blog_cats', array( 0 => $term->term_id ) );
    			
    			return array( 0 => $term->term_id );
    		}
    	}
    	
    	
    	return false;
    	
    }
    
    

    2. Let us Create the category and setup it when the group is create

    
    
    //Blog Categories for Groups, auto category creation and association
    function bcg_custom_create_group_name_category( $group_id, $group ) {
    
    	bcg_custom_create_group_category( $group );
    }
    
    add_action( 'groups_created_group', 'bcg_custom_create_group_name_category', 10, 2 );
    

    3. Disable the Blog Categories screen on Create Group and Grou Admin settings page

    
    //modify extensions to hide the create/manage screen
    function bcg_custom_modify_extension( &$extension ) {
    	
    	 $extension->enable_create_step = false; // enable create step
    	 $extension->enable_edit_item = false; // If 
    }
    add_action( 'bcg_created_group_extension', 'bcg_custom_modify_extension' );
    

    4. Modify the BCG form to add a hidden field and set post status to publish(we will use hidden field to set the forced category)

    
    
    function bcg_custom_forms_args( $args ) {
    	
    	$args['tax'] = array();
    	
    	$cusom_fields = isset( $args['custom_fields'] ) ? $args['custom_fields']: array() ;
    	
    	$group = groups_get_current_group();
    	
    	if( empty( $group ) || empty( $group->id ) ) {
    		return $args;
    	}
    	
    	$cats = bcg_get_categories( $group->id );
    	//if there is no category set
    	if( empty( $cats ) ) {
    		//create if not exists
    		$cats = bcg_custom_create_group_category( $group );
    	}
    	
    	if ( empty( $cats ) ) {
    		return $args;
    	}
    	
    	$cusom_fields['_bcg_selected_category'] = array(
    		'type'		=> 'hidden',
    		'default'	=> $cats[0],
    		'label'		=> ''	
    	);
    	
    	$args['custom_fields'] = $cusom_fields;
    	$args['post_status'] = 'publish';
    	
    	return $args;
    }
    add_filter( 'bcg_form_args', 'bcg_custom_forms_args' );
    
    

    5. Set the Category

    
    
    //set default categorywhen post is being saved
    function bcg_add_default_category_to_post( $post_id ) {
    	
    	$post = get_post( $post_id );
    	
    	if ( ! $post ) {
    		return ;
    	}
    	
    	$custom_fields = isset( $_POST['custom_fields'] ) ? $_POST['custom_fields'] : array();
    	
    	if ( empty( $custom_fields ) ) {
    		return ;
    	}
    	
    	if ( ! empty( $custom_fields[ '_bcg_selected_category'] ) ) {
    		
    		$term_id = absint( $custom_fields['_bcg_selected_category'] );
    		//i know there is a loop here that can allow other categories to be asosciated too
    		//to avoid that we need the group id in future
    		wp_set_object_terms( $post->ID, $term_id, 'category');
    	
    	}
    
    }
    
    add_action( 'bsfep_post_saved', 'bcg_add_default_category_to_post' );
    
    

    That will do everything.

    It will remove the screens and auto set category name as well as set category for posts.

    Please do let me know if that works for you or not?

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 2
    Hamish Erskine on #3038

    Thanks @sbrajesh,
    I’ll implement it soon.

    While you’re here, is there a way to easily replace “Blog” and “Post” with “story”?
    Would this need a) editing the actual plugin, or b) a translation file, or c) I could do it in bp-custom.php?

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #3039

    Hi Hamish,
    You will need to translate using languages file.

  • Participant
    Level: Initiated
    Posts: 2
    Hamish Erskine on #3056

    I’ve added the code (there is nothing else in my bp-custom.php files, and all my buddypress plugins aren’t modified).

    Currently it doesn’t work:

    1. On the second page of the sign up it still displays the checkbox : “Disable Blog Categories”.
    2. It does create a category using the group name.
    3. The “uncategoriesed” category is associated with the group (it allows you to post so there is a category associated, and when it is published it’s in the uncategorised category).

    What should I try next?

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #3057

    Hi Hamish,
    Please upgrade both the plugins.
    https://buddydev.com/plugins/bp-simple-front-end-post/
    https://buddydev.com/plugins/blog-categories-for-groups/

    Also after upgrade, Please make sure to visit Settings->BCG Settings and configure it.

    Now, coming to your issue:-
    1. That is correct, I did not provide any hook to remove that. Please add following code to remove it

    
    function bcg_remove_checkbox() {
    
    	if ( has_action( 'bp_before_group_settings_admin', 'bcg_group_disable_form' ) ) {
    		remove_action( 'bp_before_group_settings_admin', 'bcg_group_disable_form' );
    	}
    
    	if ( has_action( 'bp_before_group_settings_creation_step', 'bcg_group_disable_form' ) ) {
    		add_action( 'bp_before_group_settings_creation_step', 'bcg_group_disable_form' );
    	}
    }
    add_action( 'bp_init', 'bcg_remove_checkbox' );
    

    3. Upgrading front end post plugin and the blog categories for groups will fix the issue.

    Please update the code and plugins and let me know if that works for you or not?

    Thank you
    Brajesh

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #3540

    Closing it as resolved due to lack of reply. Please feel free to reopen if you need further assistance.

    Thank you
    Brajesh

The topic ‘ [Resolved] [Blog Categories For Groups] Auto-creating & selecting cats for groups’ is closed to new replies.

This topic is: resolved