buddydev / blog-categories-for-groups

BuddyPress Blog Categories for groups Plugin
http://buddydev.com/plugins/blog-categories-for-groups/
12 stars 6 forks source link

Allow site admin to automatically create/Use categories with Same group name #8

Open sbrajesh opened 13 years ago

sbrajesh commented 13 years ago

Allow site admins to choose if they want to automatically create/associate categories with the same name as group to the group on group creation.

Suggested by @ Sven

BenJackGill commented 4 years ago

Any progress on this? Would love for groups to automatically create and be associated with a blog category of the same name. No manual admin or user input required.

BenJackGill commented 4 years ago

Fixed it. For anyone else that comes along I found a solution on their support forum. All this code was taken from this thread: https://buddydev.com/support/forums/topic/blog-categories-for-groups-auto-creating-selecting-cats-for-groups/

Put the code in your bp-custom.php file: https://codex.buddypress.org/themes/bp-custom-php/

Here is all the code together at once:

/**
 * 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;

}

//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 );

//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' );

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' );

//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' );

//Remove BCG "Disable Blog Categories" checkbox
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' );
sbrajesh commented 4 years ago

Thank you for sharing the code. I hope it helps others.

BenJackGill commented 4 years ago

If the group gets deleted, is it possible to also automatically delete the associated category that was created for it?