Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jul 19th, 2012  |  syntax: None  |  size: 2.76 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Plugin Name: Favorite Color
  4. Plugin URI: http://MyWebsite.com
  5. Description: Let Groups Pick their favorite Color
  6. Version: 1.0
  7. Requires at least: 3.3
  8. Tested up to: 3.3.1
  9. License: GPL3
  10. Author: Your Name
  11. Author URI: http://YourCoolWebsite.com
  12. */
  13. function bp_group_meta_init() {
  14. function custom_field($meta_key='') {
  15. //get current group id and load meta_key value if passed. If not pass it blank
  16. return groups_get_groupmeta( bp_get_group_id(), $meta_key) ;
  17. }
  18. //code if using seperate files require( dirname( __FILE__ ) . '/buddypress-group-meta.php' );
  19. // This function is our custom field's form that is called in create a group and when editing group details
  20. function group_header_fields_markup() {
  21. global $bp, $wpdb;?>
  22. <label for="favorite-color">My Favorite Color</label>
  23. <input id="favorite-color" type="text" name="favorite-color" value="<?php echo custom_field('favorite-color'); ?>" />
  24. <br>
  25. <label for="favorite-color2">My Favorite Color 2</label>
  26. <input type="checkbox" name="favorite-color2" value="1" <?php if (custom_field('favorite-color2')) echo 'checked="checked"'; ?>/>
  27. <br>
  28.                                  <label>
  29.                                 <input type="radio" name="favorite-color3" value="" <?php if (!custom_field('favorite-color2')) echo 'checked="checked"'; ?> />
  30.                         Default</label>
  31.                         <br />
  32.                                 <label>
  33.                                         <input type="radio" name="favorite-color3" value="1" <?php if (custom_field('favorite-color2')) echo 'checked="checked"'; ?> />
  34.                         Other</label>
  35. <br>
  36. <?php }
  37. // This saves the custom group meta – props to Boone for the function
  38. // Where $plain_fields = array.. you may add additional fields, eg
  39. //  $plain_fields = array(
  40. //      'field-one',
  41. //      'field-two'
  42. //  );
  43. function group_header_fields_save( $group_id ) {
  44. global $bp, $wpdb;
  45. $plain_fields = array(
  46. 'favorite-color',
  47. 'favorite-color2',
  48. 'favorite-color3'
  49. );
  50. foreach( $plain_fields as $field ) {
  51. $key = $field;
  52. if ( isset( $_POST[$key] ) ) {
  53. $value = $_POST[$key];
  54. groups_update_groupmeta( $group_id, $field, $value );
  55. }
  56. }
  57. }
  58. add_filter( 'groups_custom_group_fields_editable', 'group_header_fields_markup' );
  59. add_action( 'groups_group_details_edited', 'group_header_fields_save' );
  60. add_action( 'groups_created_group',  'group_header_fields_save' );
  61.  
  62. // Show the custom field in the group header
  63. function show_field_in_header( ) {
  64. echo "<p> My favorite color is:" . custom_field('favorite-color') . "</p>";
  65.  
  66. if (custom_field('favorite-color2')) { echo '<p>off</p>'; } else { echo '<p>on</p>'; };
  67.  
  68. if (custom_field('favorite-color2')) { echo '<p>Other</p>'; } else { echo '<p>Default</p>'; };
  69. }
  70. add_action('bp_group_header_meta' , 'show_field_in_header') ;
  71. }
  72. add_action( 'bp_include', 'bp_group_meta_init' );
  73. /* If you have code that does not need BuddyPress to run, then add it here. */
  74. ?>
clone this paste RAW Paste Data