sbrajesh

Edit profile under settings

Aug 19th, 2015
132
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. function buddydev_modify_user_nav() {
  4.        
  5.     $bp = buddypress();
  6.    
  7.     // rename general settings tab
  8.     $bp->bp_options_nav['settings']['general']['name'] = 'Account Details';
  9.     // remove profile menu tab
  10.     //remove edit/change avatar from profile sub menu  
  11.     //unset($bp->bp_options_nav['profile']['edit']);
  12.     //unset($bp->bp_options_nav['profile']['change-avatar']);
  13.    
  14.     bp_core_remove_subnav_item( $bp->profile->slug, 'edit' );
  15.     bp_core_remove_subnav_item( $bp->profile->slug, 'change-avatar' );
  16.    
  17.     $user_url = bp_loggedin_user_domain();
  18.     // add 'Change profile picture' sub-menu tab
  19.     bp_core_new_subnav_item( array(
  20.             'name'              => 'Change Profile Picture',
  21.             'slug'              => 'change-profile-picture',
  22.             'parent_url'        => trailingslashit( $user_url . $bp->settings->slug ),
  23.             'parent_slug'       => $bp->settings->slug,
  24.             'screen_function'   => 'buddydev_modified_avatar_screen_handler',
  25.             'position' => 30,
  26.             'user_has_access'   => bp_is_my_profile()
  27.             )
  28.     );
  29.     //add edit profile sub menu under settings
  30.     bp_core_new_subnav_item( array(
  31.             'name'              => 'Edit Profile',
  32.             'slug'              => 'edit-profile',
  33.             'parent_url'        => trailingslashit( $user_url . $bp->settings->slug ),
  34.             'parent_slug'       => $bp->settings->slug,
  35.             'screen_function'   => 'buddydev_modified_edit_profile_screen_handler',
  36.             'position'          => 40,
  37.             'user_has_access'   => bp_is_my_profile()
  38.             )
  39.     );
  40. }
  41.  
  42. add_action( 'bp_setup_nav', 'buddydev_modify_user_nav', 100 );
  43.  
  44.  
  45.  //modified screen handler
  46. function buddydev_modified_avatar_screen_handler(){
  47.     //hook custom function to get theh screen content on the plugins template    
  48.     add_action( 'bp_template_content', 'buddydev_screen_content_change_avatar' );
  49.     xprofile_screen_change_avatar();        
  50. }
  51. //load plugins template on change avatar
  52. function buddydev_filter_change_avatar_template( $template ) {
  53.    
  54.     return 'members/single/plugins';
  55. }
  56. add_filter('xprofile_template_change_avatar', 'buddydev_filter_change_avatar_template' );
  57.  
  58.  
  59. //ask BuddyPress to show the change-avatar file content using plugins template
  60. function buddydev_screen_content_change_avatar() {
  61.     bp_get_template_part( 'members/single/profile/change-avatar' );
  62. }
  63.  
  64. //For Edit profile, It is a little bit tricky as BuddyPress uses the profile/edit url a lot
  65. function buddydev_modified_edit_profile_screen_handler() {
  66.     //if the group is not set, redirect to first group
  67.     if ( ! bp_action_variable( 1 ) ) {
  68.         bp_core_redirect( trailingslashit( bp_displayed_user_domain() . buddypress()->settings->slug . '/edit-profile/group/1' ) );
  69.     }
  70.    
  71.     add_action( 'bp_template_content', 'buddydev_screen_content_edit_profile' );
  72.            
  73.     xprofile_screen_edit_profile();
  74. }
  75.  
  76. //filter xprofile template, ask buddypress to load plugins template
  77. function buddydev_filter_edit_profile_template( $template ) {
  78.    
  79.     return 'members/single/plugins';
  80. }
  81. add_filter( 'xprofile_template_edit_profile', 'buddydev_filter_edit_profile_template' );
  82.  
  83. //show the edit profile content
  84. function buddydev_screen_content_edit_profile() {
  85.    
  86.     bp_get_template_part( 'members/single/profile/edit' );
  87. }
  88.  
  89. //now, all the filters below modify various Profile specific url to go to settings
  90.  
  91. //modify edit profile form action url to point to settings screen url
  92. function buddydev_filter_edit_profile_form_url( $url ) {
  93.     global $group;
  94.     return trailingslashit( bp_displayed_user_domain() . buddypress()->settings->slug . '/edit-profile/group/' . $group->id );
  95. }
  96. add_filter( 'bp_get_the_profile_group_edit_form_action', 'buddydev_filter_edit_profile_form_url' );
  97.  
  98.  
  99. //filter the Profile Field group link to point to settings url
  100. function buddydev_filter_edit_profile_group_tabs( $tabs, $groups, $group_name ) {
  101.     //instead of processing, let us go with string replamenet :)
  102.    
  103.     $profile_url = trailingslashit( bp_displayed_user_domain() . buddypress()->profile->slug . '/edit/group/' );
  104.     $settings_url = trailingslashit( bp_displayed_user_domain() . buddypress()->settings->slug . '/edit-profile/group/' );
  105.    
  106.     $tabs = str_replace(  $profile_url, $settings_url, $tabs );
  107.    
  108.     return $tabs;
  109. }
  110. add_filter( 'xprofile_filter_profile_group_tabs', 'buddydev_filter_edit_profile_group_tabs', 10, 3 );
  111.  
  112.  
  113. //we use this to redirect early
  114. function buddydev_redirect_on_profile_update (  $user_id, $posted_field_ids, $errors, $old_values, $new_values ) {
  115.  
  116.     if ( ! empty( $errors ) ) {
  117.                 bp_core_add_message( __( 'There was a problem updating some of your profile information. Please try again.', 'buddypress' ), 'error' );
  118.     } else {
  119.                 bp_core_add_message( __( 'Changes saved.', 'buddypress' ) );
  120.     }
  121.  
  122.     // Redirect back to the edit screen to display the updates and message
  123.     bp_core_redirect( trailingslashit( bp_displayed_user_domain() . buddypress()->settings->slug . '/edit-profile/group/' . bp_action_variable( 1 ) ) );
  124. }
  125. add_action( 'xprofile_updated_profile', 'buddydev_redirect_on_profile_update', 10, 5 );
  126.  
  127.  
  128. //and the last but the most important one, BuddyPress will not show empty groups on settings/edit profile, we force it
  129.  
  130. function buddydev_modify_has_profile_args( $args ) {
  131.    
  132.     if( bp_is_settings_component() && bp_is_current_action( 'edit-profile') ) {
  133.         $args['hide_empty_groups'] = false;
  134.         $args['hide_empty_fields'] = false;
  135.     }
  136.    
  137.     return $args;
  138. }
  139. add_filter( 'bp_after_has_profile_parse_args', 'buddydev_modify_has_profile_args' );
RAW Paste Data