WordPress, BuddyPress & Events Manager customisations

Below are some of the pieces of custom code I’ve found around the internet for use on the WordPress, BuddyPress and Events Manager site I run, used to create a social network for a local hiking group.

/*
* Removes the wordpress News metabox
* see https://codex.wordpress.org/Dashboard_Widgets_API for further details
*/
function yoursite_remove_dashboard_meta() {
  remove_meta_box( 'dashboard_primary', 'dashboard', 'normal' );
}
add_action( 'admin_init', 'yoursite_remove_dashboard_meta' );
/*
* Changes the logon screen image to the url you specify
* assuming you are using a sub theme this folder goes in there
*/
function yoursite_login_logo() { ?>
  <style type="text/css">
  #login h1 a, .login h1 a {
    background-image: url(<?php echo get_stylesheet_directory_uri(); ?>/images/yourlogo.png);
    background-size: 289px 289px;
    height: 289px;
    width: auto;
  }
  </style>
  <?php 
}
add_action( 'login_enqueue_scripts', 'yoursite_login_logo' );
/*
* Changes the image link on login page to take back to homepage
* and the hover text
*/
function yoursite_login_logo_url() {
  return home_url();
}
add_filter( 'login_headerurl', 'yoursite_login_logo_url' );

function yoursite_login_logo_url_title() {
  return 'Your Site Title';
}
add_filter( 'login_headertitle', 'yoursite_login_logo_url_title' );
/*
* Add custom message to WordPress login page
*/
function yoursite_login_message( $message ) {
  if ( empty($message) ){
    return '<p><strong>Looking for more info about the site? <a href="https://your.site/more-info/">Click here</a></strong></p>';
  } else {
    return $message;
  }
}
add_filter( 'login_message', 'yoursite_login_message' );
/*
*  Gallery Default Settings changes to 4 wide
*/
function yoursite_theme_gallery_defaults( $settings ) {
  $settings['galleryDefaults']['columns'] = 4;
  return $settings;
}
add_filter( 'media_view_settings', 'yoursite_theme_gallery_defaults' );
/*
* Removes extra unused boxes from event creation pages
* https://codex.wordpress.org/Function_Reference/remove_meta_box
* for more options
*/
function yoursite_remove_meta_boxes() {
  if ( ! current_user_can( 'manage_options' ) ) {
    remove_meta_box( 'postexcerpt', 'post', 'normal' );
    remove_meta_box( 'slugdiv', 'post', 'normal' );
    remove_meta_box( 'revisionsdiv', 'post', 'normal' );
    remove_meta_box( 'authordiv', 'post', 'normal' );
    remove_meta_box( 'trackbacksdiv', 'post', 'normal' );
    remove_meta_box( 'postcustom', 'post', 'normal' );
    remove_meta_box( 'tagsdiv-post_tag', 'post', 'normal' );
    remove_meta_box( 'slugdiv', 'event', 'normal' );
    remove_meta_box( 'postexcerpt', 'event', 'normal' );
  }
}
add_action( 'admin_menu', 'yoursite_remove_meta_boxes' );
/*
* Events -> Settings -> General -> General Options -> Event Attributes
* allows the addion of extra attributes
* The following code checks to see if that attribute has been set for the
* event and hides it if not.
* @param string $replacement
* @param string $condition
* @param string $match
* @param object $EM_Event
* @return string
*/
function yoursite_filterEventOutputCondition($replacement, $condition, $match, $EM_Event){
  if (is_object($EM_Event)) {
    switch ($condition) {
    // replace LF with HTML line breaks
    case 'nl2br':
      // remove conditional
        $replacement = preg_replace('/\{\/?nl2br\}/', '', $match);
      // process any placeholders and replace LF
        $replacement = nl2br($EM_Event->output($replacement));
    break;

    // #_ATT{your_attribute_1}
    case 'has_att_your_attribute_1':
      if (is_array($EM_Event->event_attributes) && !empty($EM_Event->event_attributes['your_attribute_1']))
        $replacement = preg_replace('/\{\/?your_attribute_1\}/', '', $match);
      else
        $replacement = '';
      break;

    // #_ATT{your_attribute_2}
    case 'has_att_your_attribute_2':
      if (is_array($EM_Event->event_attributes) && !empty($EM_Event->event_attributes['your_attribute_2']))
        $replacement = preg_replace('/\{\/?your_attribute_2\}/', '', $match);
      else
        $replacement = '';
    break;

    // #_ATT{your_attribute_3}
    case 'has_att_your_attribute_3':
      if (is_array($EM_Event->event_attributes) && !empty($EM_Event->event_attributes['your_attribute_3']))
        $replacement = preg_replace('/\{\/?your_attribute_3\}/', '', $match);
      else
        $replacement = '';
    break;
    }
  }
  return $replacement;
}
add_filter('em_event_output_condition', 'yoursite_filterEventOutputCondition', 10, 4);
/* 
* This changes the default ticket name to Basic, leaves the description blank and sets
* the default number of tickets to 12
*/
function yoursite_em_add_default_tickets($tickets, $EM_Bookings, $force_reload = false) {
  if ( empty($tickets->tickets) ) {
    $ticket_data = array();
    $ticket_data[0] = array('ticket_name' => 'Basic', 'ticket_description' => '', 'ticket_spaces' => 12, 'ticket_price' => 0 );
    if (is_array($tickets->tickets)) unset($tickets->tickets);
      foreach ($ticket_data as $ticket) {
        $EM_Ticket = new EM_Ticket($ticket);
        $tickets->tickets[] = $EM_Ticket;
    }
  }
  return $tickets;
}
add_filter('em_bookings_get_tickets', 'yoursite_em_add_default_tickets', 10, 2);
/*
* Changes role names
*/
function yoursite_change_role_name() {
  global $wp_roles;
  if ( ! isset( $wp_roles ) )
    $wp_roles = new WP_Roles();
    $wp_roles->roles['editor']['name'] = 'King_Queen';
    $wp_roles->role_names['editor'] = 'King_Queen';
    $wp_roles->roles['author']['name'] = 'Prince_Princess';
    $wp_roles->role_names['author'] = 'Prince_Princess';
    $wp_roles->roles['contributor']['name'] = 'Duke_Duchess';
    $wp_roles->role_names['contributor'] = 'Duke_Duchess';
    $wp_roles->roles['subscriber']['name'] = 'Jester';
    $wp_roles->role_names['subscriber'] = 'Jester';
}
add_action('init', 'yoursite_change_role_name');
/*
* Display user role on their profile, total of events created and attended
*/

add_action( 'bp_before_member_header_meta', 'yoursite_show_role_on_profile' );
function yoursite_show_role_on_profile() {
  global $wp_roles;
  $user_id = bp_displayed_user_id();
  $user = get_userdata($user_id);
  $roles = $user->roles;

  if ( !$roles )
    return;

  if ( !isset( $wp_roles ) )
    $wp_roles = new WP_Roles();
  $named_roles = array();
  foreach ( $roles as $role ) {
    $named_roles [] = $wp_roles->role_names[$role];
  }

  $user_id = bp_displayed_user_id();
  $counts = yoursite_get_author_event_counts($user_id);
  if (isset($counts))
    $custom_column[] = "{$counts}";
  if (empty($custom_column)){
    $custom_column = ", no events created yet";
  }
  else
    $custom_column = " {$counts[$user_id]['count']} and has created {$custom_column} event(s)";
  $results = yoursite_get_total_attended_events($user_id);

  if ( $named_roles )
    echo '<span class="user-role activity"> - ' . $named_roles[0] . $custom_column . ', attended ' . $results . ' in total.</span>';
}

function yoursite_get_total_attended_events($user_id) {
  static $counts;

  if (!isset($counts)) {
    global $wpdb;
    global $wp_post_types;
    $sql = <<<SQL
    SELECT
    COUNT(*) AS total
    FROM
    ENTER_YOUR_BOOKING_TABLE_NAME_HERE //need to find a better way for this rather than hardcoding
    WHERE person_id = $user_id
    AND booking_status = 1
    SQL;
    $something = $wpdb->get_results($sql);
    return $something[0]->total;
  }
}

function yoursite_get_author_event_counts($user_id) {
  static $counts;
  if (!isset($counts)) {
    global $wpdb;
    global $wp_post_types;
    $sql = <<<SQL
    SELECT
    post_type,
    post_author,
    COUNT(*) AS post_count
    FROM
    {$wpdb->posts}
    WHERE post_author = $user_id
    AND post_type ='event'
    AND post_status = 'publish'
    GROUP BY
    post_type,
    post_author
    SQL;

    $posts = $wpdb->get_results($sql);

    return $posts[0]->post_count;

  }
}
/**
* Remove powered by wordpress
*/
add_filter( 'tc_wp_powered', 'yoursite_wp_powered' );
function yoursite_wp_powered() {
  $text_output = '';
  return $text_output;
}

/*
* Adds events to the activity stream, multiple options
* can be added to the array as required
*/
function yoursite_record_more_types( $types ) {
  $types[] = 'event';
  return $types;
}
add_filter( 'bp_blogs_record_post_post_types', 'yoursite_record_more_types');

/*
 *  Ensures the date for comments is the comment date and not
 *  the original post date
 */
function yoursite_bpfr_stream( $qs, $object ) {
	if ( 'activity' != $object ) {
		return $qs;
	}
	
	$qs = wp_parse_args( $qs, array() );
	$qs['display_comments'] = 'stream';
	
	return $qs;
}
add_filter( 'bp_ajax_querystring', 'yoursite_bpfr_stream', 20, 2 );

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.