BuddyPress Create Admin Screen
•4 min read
buddypresswordpressphp
BuddyPress lists the groups in the admin but you can oly create a group from the front. This code adds a missing create group button and screen to allow admins to create groups from admin.
<?php
/**
* BuddyPress Groups create admin screen.
*
* @package BuddyPress
* @subpackage Groups
*/
// Exit if accessed directly. defined( 'ABSPATH' ) || exit;
// The per_page screen option. Has to be hooked in extremely early. if ( is_admin() && ! empty( $_REQUEST['page'] ) && 'bp-groups-create' == $_REQUEST['page'] ) { add_filter( 'set-screen-option', 'bp_groups_admin_screen_options', 10, 3 ); }
/** * Register the Groups component admin screen. * */ function bp_create_groups_add_admin_menu() {
// Add our screen. $hook = add_submenu_page( 'bp-groups', _x( 'Add New Group', 'Admin Groups page title', 'buddypress' ), _x( 'Add New', 'Admin Groups menu', 'buddypress' ), 'bp_moderate', 'bp-groups-create', 'bp_create_groups_admin' );
// Hook into early actions to load custom CSS and our init handler. add_action( "load-$hook", 'bp_create_groups_admin_load' ); } add_action( bp_core_admin_hook(), 'bp_create_groups_add_admin_menu', 999 );
/** * Add group create meta boxes. * * @return void */ function bp_create_groups_admin_load() { add_meta_box( 'submitdiv', _x( 'Create', 'group admin edit screen', 'buddypress' ), 'bp_create_groups_admin_edit_metabox_status', get_current_screen()->id, 'side', 'high' ); add_meta_box( 'bp_group_settings', _x( 'Settings', 'group admin edit screen', 'buddypress' ), 'bp_groups_admin_create_metabox_settings', get_current_screen()->id, 'side', 'core' ); }
/** * Group create main metabox output. * * @return void */ function bp_create_groups_admin() {
if ( ! bp_current_user_can( 'bp_moderate' ) ) { die( '-1' ); }
?>
<style> #bp_groups_name input, #bp_groups_name textarea { width: 100%; } .bp-groups-settings-section label { width: 100%; display: block; padding: 5px; } .bp-groups-settings-section legend { font-weight: 600; } </style>
<div class="wrap"> <?php if ( version_compare( $GLOBALS['wp_version'], '4.8', '>=' ) ) : ?>
<h1 class="wp-heading-inline"><?php _e( 'Add New Group', 'buddypress' ); ?></h1>
<hr class="wp-header-end">
<?php else : ?>
<h1><?php _e( 'Add New Group', 'buddypress' ); ?>
</h1>
<?php endif; ?>
<form action="" id="bp-groups-create-form" method="post"> <div id="poststuff">
<div id="post-body" class="metabox-holder columns-<?php echo 1 === get_current_screen()->get_columns() ? '1' : '2'; ?>"> <div id="post-body-content"> <div id="postdiv"> <div id="bp_groups_name" class="postbox"> <div class="inside"> <h2><?php _e( 'Name *', 'buddypress' ); ?></h2> <label for="bp-groups-name" class="screen-reader-text"> <?php /* translators: accessibility text */ _e( 'Group Name', 'buddypress' ); ?> </label> <input type="text" name="bp-groups-name" id="bp-groups-name" value="" />
<h2><?php _e( 'Description *', 'buddypress' ); ?></h2> <label for="bp-groups-description" class="screen-reader-text"> <?php /* translators: accessibility text */ _e( 'Group Description', 'buddypress' ); ?> </label> <textarea name="bp-groups-description"></textarea> </div> </div> </div> </div><!-- #post-body-content -->
<div id="postbox-container-1" class="postbox-container"> <?php do_meta_boxes( get_current_screen()->id, 'side', null ); ?> </div>
<div id="postbox-container-2" class="postbox-container"> <?php do_meta_boxes( get_current_screen()->id, 'normal', null ); ?> <?php do_meta_boxes( get_current_screen()->id, 'advanced', null ); ?> </div> </div><!-- #post-body -->
</div><!-- #poststuff --> <?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?> <?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?> <?php wp_nonce_field( 'create-group_new' ); ?> </form>
</div><!-- .wrap -->
<script> (function($) { $('#bp-groups-create-form').on( 'submit', function(e) { e.preventDefault();
var data = {}; $(e.target).serializeArray().map(function(x){data[x.name] = x.value;}); console.log(data);
if ( '' === data['bp-groups-description'] || '' === data['bp-groups-name'] ) { alert('Please fill in required fields.'); return; }
$('#create-action .spinner').addClass('is-active');
$.ajax({ url: '<?php echo esc_url( site_url() ); ?>/wp-json/buddypress/v1/groups', beforeSend: function ( xhr ) { xhr.setRequestHeader( 'X-WP-Nonce', '<?php echo wp_create_nonce( 'wp_rest' ); ?>' ); }, data: { name: data['bp-groups-name'], description: data['bp-groups-description'], status: data['group-status'] ? data['group-status'] : 'public' }, error: function(e) { console.log('error', e); }, success: function(data) { console.log(data); if(data) { var path = 'admin.php?page=bp-groups&gid=' + data[0]['id'] + '&action=edit'; window.location.href = '<?php echo esc_url( admin_url() ); ?>' + path; } }, type: 'POST' });
});
})(jQuery); </script>
<?php }
/** * Renders the Status metabox for the Groups admin edit screen. * * @return void */ function bp_create_groups_admin_edit_metabox_status() { $base_url = add_query_arg( array( 'page' => 'bp-groups-create', ), bp_get_admin_url( 'admin.php' ) ); ?>
<div id="submitcomment" class="submitbox"> <div id="major-publishing-actions"> <div id="create-action" style="float: left;"> <div class="spinner"></div> </div> <div id="publishing-action"> <?php submit_button( __( 'Create Group', 'buddypress' ), 'primary', 'save', false ); ?> </div> <div class="clear"></div> </div><!-- #major-publishing-actions --> </div><!-- #submitcomment -->
<?php }
/** * Group create settings metabox output. * * @return void */ function bp_groups_admin_create_metabox_settings() {
?>
<?php if ( bp_is_active( 'forums' ) ) : ?> <div class="bp-groups-settings-section" id="bp-groups-settings-section-forum"> <label for="group-show-forum"><input type="checkbox" name="group-show-forum" id="group-show-forum" /> <?php _e( 'Enable discussion forum', 'buddypress' ); ?></label> </div> <?php endif; ?>
<div class="bp-groups-settings-section" id="bp-groups-settings-section-status"> <fieldset> <legend><?php _e( 'Privacy', 'buddypress' ); ?></legend>
<label for="bp-group-status-public"><input type="radio" name="group-status" id="bp-group-status-public" value="public" /><?php _e( 'Public', 'buddypress' ); ?></label> <label for="bp-group-status-private"><input type="radio" name="group-status" id="bp-group-status-private" value="private" /><?php _e( 'Private', 'buddypress' ); ?></label> <label for="bp-group-status-hidden"><input type="radio" name="group-status" id="bp-group-status-hidden" value="hidden" /><?php _e( 'Hidden', 'buddypress' ); ?></label> </fieldset> </div>
<?php
}