bp_core_add_page_mappings

Creates necessary directory pages.

Description

bp_core_add_page_mappings( (array) $components, (string) $existing = 'keep' ); 

Directory pages are those WordPress pages used by BP components to display content (eg, the groups page created by BP).

Parameters (2)

0. $components (array)
Components to create pages for.
1. $existing — Optional. (string) => 'keep'
'delete' if you want to delete existing page mappings and replace with new ones. Otherwise existing page mappings are kept, and the gaps filled in with new pages. Default: keep..

Usage

  1. if ( !function_exists( 'bp_core_add_page_mappings' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-functions.php'; 
  3.  
  4. // Components to create pages for. 
  5. $components = array(); 
  6. $existing = 'keep'; 
  7.  
  8. // NOTICE! Understand what this does before running. 
  9. $result = bp_core_add_page_mappings($components, $existing); 
  10.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-functions.php  
  1. function bp_core_add_page_mappings( $components, $existing = 'keep' ) { 
  2.  
  3. // If no value is passed, there's nothing to do. 
  4. if ( empty( $components ) ) { 
  5. return; 
  6.  
  7. // Make sure that the pages are created on the root blog no matter which 
  8. // dashboard the setup is being run on. 
  9. if ( ! bp_is_root_blog() ) { 
  10.  
  11. $pages = bp_core_get_directory_page_ids( 'all' ); 
  12.  
  13. // Delete any existing pages. 
  14. if ( 'delete' === $existing ) { 
  15. foreach ( (array) $pages as $page_id ) { 
  16. wp_delete_post( $page_id, true ); 
  17.  
  18. $pages = array(); 
  19.  
  20.  
  21. $pages_to_create = array(); 
  22. foreach ( array_keys( $components ) as $component_name ) { 
  23. if ( ! isset( $pages[ $component_name ] ) && isset( $page_titles[ $component_name ] ) ) { 
  24. $pages_to_create[ $component_name ] = $page_titles[ $component_name ]; 
  25.  
  26. // Register and Activate are not components, but need pages when 
  27. // registration is enabled. 
  28. if ( bp_get_signup_allowed() ) { 
  29. foreach ( array( 'register', 'activate' ) as $slug ) { 
  30. if ( ! isset( $pages[ $slug ] ) ) { 
  31. $pages_to_create[ $slug ] = $page_titles[ $slug ]; 
  32.  
  33. // No need for a Sites directory unless we're on multisite. 
  34. if ( ! is_multisite() && isset( $pages_to_create['blogs'] ) ) { 
  35. unset( $pages_to_create['blogs'] ); 
  36.  
  37. // Members must always have a page, no matter what. 
  38. if ( ! isset( $pages['members'] ) && ! isset( $pages_to_create['members'] ) ) { 
  39. $pages_to_create['members'] = $page_titles['members']; 
  40.  
  41. // Create the pages. 
  42. foreach ( $pages_to_create as $component_name => $page_name ) { 
  43. $exists = get_page_by_path( $component_name ); 
  44.  
  45. // If page already exists, use it. 
  46. if ( ! empty( $exists ) ) { 
  47. $pages[ $component_name ] = $exists->ID; 
  48. } else { 
  49. $pages[ $component_name ] = wp_insert_post( array( 
  50. 'comment_status' => 'closed',  
  51. 'ping_status' => 'closed',  
  52. 'post_status' => 'publish',  
  53. 'post_title' => $page_name,  
  54. 'post_type' => 'page',  
  55. ) ); 
  56.  
  57. // Save the page mapping. 
  58. bp_update_option( 'bp-pages', $pages ); 
  59.  
  60. // If we had to switch_to_blog, go back to the original site. 
  61. if ( ! bp_is_root_blog() ) {