bbp_template_include_theme_compat

Reset main query vars and filter 'the_content' to output a bbPress template part as needed.

Description

bbp_template_include_theme_compat( (string) $template = '' ); 

Parameters (1)

0. $template — Optional. (string) => ''
The template.

Usage

  1. if ( !function_exists( 'bbp_template_include_theme_compat' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/core/theme-compat.php'; 
  3.  
  4. // The template. 
  5. $template = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bbp_template_include_theme_compat($template); 
  9.  

Defined (1)

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

/includes/core/theme-compat.php  
  1. function bbp_template_include_theme_compat( $template = '' ) { 
  2.  
  3. /** 
  4. * Bail if a root template was already found. This prevents unintended 
  5. * recursive filtering of 'the_content'. 
  6. * @link http://bbpress.trac.wordpress.org/ticket/2429 
  7. */ 
  8. return $template; 
  9.  
  10. /** 
  11. * If BuddyPress is activated at a network level, the action order is 
  12. * reversed, which causes the template integration to fail. If we're looking 
  13. * at a BuddyPress page here, bail to prevent the extra processing. 
  14. * This is a bit more brute-force than is probably necessary, but gets the 
  15. * job done while we work towards something more elegant. 
  16. */ 
  17. if ( function_exists( 'is_buddypress' ) && is_buddypress() ) 
  18. return $template; 
  19.  
  20. // Define local variable(s) 
  21. $bbp_shortcodes =bbpress)->shortcodes; 
  22.  
  23. // Bail if shortcodes are unset somehow 
  24. if ( !is_a( $bbp_shortcodes, 'BBP_Shortcodes' ) ) 
  25. return $template; 
  26.  
  27. /** Users *************************************************************/ 
  28.  
  29.  
  30. // Reset post 
  31. 'ID' => 0,  
  32. 'post_author' => 0,  
  33. 'post_date' => 0,  
  34. 'post_content' => bbp_buffer_template_part( 'content', 'single-user', false ),  
  35. 'post_type' => '',  
  36. 'post_title' => bbp_get_displayed_user_field( 'display_name' ),  
  37. 'post_status' => bbp_get_public_status_id(),  
  38. 'is_archive' => false,  
  39. 'comment_status' => 'closed' 
  40. ) ); 
  41.  
  42. /** Forums ************************************************************/ 
  43.  
  44. // Forum archive 
  45. } elseif ( bbp_is_forum_archive() ) { 
  46.  
  47. // Page exists where this archive should be 
  48.  
  49. // Should we replace the content... 
  50. if ( empty( $page->post_content ) ) { 
  51.  
  52. // Use the topics archive 
  53. if ( 'topics' === bbp_show_on_root() ) { 
  54. $new_content = $bbp_shortcodes->display_topic_index(); 
  55.  
  56. // No page so show the archive 
  57. } else { 
  58. $new_content = $bbp_shortcodes->display_forum_index(); 
  59.  
  60. // ...or use the existing page content? 
  61. } else { 
  62. $new_content = apply_filters( 'the_content', $page->post_content ); 
  63.  
  64. // Should we replace the title... 
  65. if ( empty( $page->post_title ) ) { 
  66.  
  67. // Use the topics archive 
  68. if ( 'topics' === bbp_show_on_root() ) { 
  69. $new_title = bbp_get_topic_archive_title(); 
  70.  
  71. // No page so show the archive 
  72. } else { 
  73. $new_title = bbp_get_forum_archive_title(); 
  74.  
  75. // ...or use the existing page title? 
  76. } else { 
  77. $new_title = apply_filters( 'the_title', $page->post_title ); 
  78.  
  79. // Reset post 
  80. 'ID' => !empty( $page->ID ) ? $page->ID : 0,  
  81. 'post_title' => $new_title,  
  82. 'post_author' => 0,  
  83. 'post_date' => 0,  
  84. 'post_content' => $new_content,  
  85. 'post_type' => bbp_get_forum_post_type(),  
  86. 'post_status' => bbp_get_public_status_id(),  
  87. 'is_archive' => true,  
  88. 'comment_status' => 'closed' 
  89. ) ); 
  90.  
  91. // Single Forum 
  92. } elseif ( bbp_is_forum_edit() ) { 
  93.  
  94. // Reset post 
  95. 'ID' => bbp_get_forum_id(),  
  96. 'post_title' => bbp_get_forum_title(),  
  97. 'post_author' => bbp_get_forum_author_id(),  
  98. 'post_date' => 0,  
  99. 'post_content' => $bbp_shortcodes->display_forum_form(),  
  100. 'post_type' => bbp_get_forum_post_type(),  
  101. 'post_status' => bbp_get_forum_visibility(),  
  102. 'is_single' => true,  
  103. 'comment_status' => 'closed' 
  104. ) ); 
  105.  
  106. } elseif ( bbp_is_single_forum() ) { 
  107.  
  108. // Reset post 
  109. 'ID' => bbp_get_forum_id(),  
  110. 'post_title' => bbp_get_forum_title(),  
  111. 'post_author' => bbp_get_forum_author_id(),  
  112. 'post_date' => 0,  
  113. 'post_content' => $bbp_shortcodes->display_forum( array( 'id' => bbp_get_forum_id() ) ),  
  114. 'post_type' => bbp_get_forum_post_type(),  
  115. 'post_status' => bbp_get_forum_visibility(),  
  116. 'is_single' => true,  
  117. 'comment_status' => 'closed' 
  118. ) ); 
  119.  
  120. /** Topics ************************************************************/ 
  121.  
  122. // Topic archive 
  123. } elseif ( bbp_is_topic_archive() ) { 
  124.  
  125. // Page exists where this archive should be 
  126.  
  127. // Should we replace the content... 
  128. if ( empty( $page->post_content ) ) { 
  129. $new_content = $bbp_shortcodes->display_topic_index(); 
  130.  
  131. // ...or use the existing page content? 
  132. } else { 
  133. $new_content = apply_filters( 'the_content', $page->post_content ); 
  134.  
  135. // Should we replace the title... 
  136. if ( empty( $page->post_title ) ) { 
  137. $new_title = bbp_get_topic_archive_title(); 
  138.  
  139. // ...or use the existing page title? 
  140. } else { 
  141. $new_title = apply_filters( 'the_title', $page->post_title ); 
  142.  
  143. // Reset post 
  144. 'ID' => !empty( $page->ID ) ? $page->ID : 0,  
  145. 'post_title' => bbp_get_topic_archive_title(),  
  146. 'post_author' => 0,  
  147. 'post_date' => 0,  
  148. 'post_content' => $new_content,  
  149. 'post_type' => bbp_get_topic_post_type(),  
  150. 'post_status' => bbp_get_public_status_id(),  
  151. 'is_archive' => true,  
  152. 'comment_status' => 'closed' 
  153. ) ); 
  154.  
  155. // Single Topic 
  156. } elseif ( bbp_is_topic_edit() || bbp_is_single_topic() ) { 
  157.  
  158. // Split 
  159. if ( bbp_is_topic_split() ) { 
  160. $new_content = bbp_buffer_template_part( 'form', 'topic-split', false ); 
  161.  
  162. // Merge 
  163. } elseif ( bbp_is_topic_merge() ) { 
  164. $new_content = bbp_buffer_template_part( 'form', 'topic-merge', false ); 
  165.  
  166. // Edit 
  167. } elseif ( bbp_is_topic_edit() ) { 
  168. $new_content = $bbp_shortcodes->display_topic_form(); 
  169.  
  170. // Single 
  171. } else { 
  172. $new_content = $bbp_shortcodes->display_topic( array( 'id' => bbp_get_topic_id() ) ); 
  173.  
  174. // Reset post 
  175. 'ID' => bbp_get_topic_id(),  
  176. 'post_title' => bbp_get_topic_title(),  
  177. 'post_author' => bbp_get_topic_author_id(),  
  178. 'post_date' => 0,  
  179. 'post_content' => $new_content,  
  180. 'post_type' => bbp_get_topic_post_type(),  
  181. 'post_status' => bbp_get_topic_status(),  
  182. 'is_single' => true,  
  183. 'comment_status' => 'closed' 
  184. ) ); 
  185.  
  186. /** Replies ***********************************************************/ 
  187.  
  188. // Reply archive 
  189. } elseif ( is_post_type_archive( bbp_get_reply_post_type() ) ) { 
  190.  
  191. // Reset post 
  192. 'ID' => 0,  
  193. 'post_title' => __( 'Replies', bbpress ),  
  194. 'post_author' => 0,  
  195. 'post_date' => 0,  
  196. 'post_content' => $bbp_shortcodes->display_reply_index(),  
  197. 'post_type' => bbp_get_reply_post_type(),  
  198. 'post_status' => bbp_get_public_status_id(),  
  199. 'comment_status' => 'closed' 
  200. ) ); 
  201.  
  202. // Single Reply 
  203. } elseif ( bbp_is_reply_edit() || bbp_is_single_reply() ) { 
  204.  
  205. // Move 
  206. if ( bbp_is_reply_move() ) { 
  207. $new_content = bbp_buffer_template_part( 'form', 'reply-move', false ); 
  208.  
  209. // Edit 
  210. } elseif ( bbp_is_reply_edit() ) { 
  211. $new_content = $bbp_shortcodes->display_reply_form(); 
  212.  
  213. // Single 
  214. } else { 
  215. $new_content = $bbp_shortcodes->display_reply( array( 'id' => get_the_ID() ) ); 
  216.  
  217. // Reset post 
  218. 'ID' => bbp_get_reply_id(),  
  219. 'post_title' => bbp_get_reply_title(),  
  220. 'post_author' => bbp_get_reply_author_id(),  
  221. 'post_date' => 0,  
  222. 'post_content' => $new_content,  
  223. 'post_type' => bbp_get_reply_post_type(),  
  224. 'post_status' => bbp_get_reply_status(),  
  225. 'comment_status' => 'closed' 
  226. ) ); 
  227.  
  228. /** Views *************************************************************/ 
  229.  
  230. } elseif ( bbp_is_single_view() ) { 
  231.  
  232. // Reset post 
  233. 'ID' => 0,  
  234. 'post_title' => bbp_get_view_title(),  
  235. 'post_author' => 0,  
  236. 'post_date' => 0,  
  237. 'post_content' => $bbp_shortcodes->display_view( array( 'id' => get_query_var( bbp_get_view_rewrite_id() ) ) ),  
  238. 'post_type' => '',  
  239. 'post_status' => bbp_get_public_status_id(),  
  240. 'comment_status' => 'closed' 
  241. ) ); 
  242.  
  243. /** Search ************************************************************/ 
  244.  
  245. } elseif ( bbp_is_search() ) { 
  246.  
  247. // Reset post 
  248. 'ID' => 0,  
  249. 'post_title' => bbp_get_search_title(),  
  250. 'post_author' => 0,  
  251. 'post_date' => 0,  
  252. 'post_content' => $bbp_shortcodes->display_search( array( 'search' => get_query_var( bbp_get_search_rewrite_id() ) ) ),  
  253. 'post_type' => '',  
  254. 'post_status' => bbp_get_public_status_id(),  
  255. 'comment_status' => 'closed' 
  256. ) ); 
  257.  
  258. /** Topic Tags ********************************************************/ 
  259.  
  260. // Topic Tag Edit 
  261. } elseif ( bbp_is_topic_tag_edit() || bbp_is_topic_tag() ) { 
  262.  
  263. // Stash the current term in a new var 
  264. set_query_var( 'bbp_topic_tag', get_query_var( 'term' ) ); 
  265.  
  266. // Show topics of tag 
  267. if ( bbp_is_topic_tag() ) { 
  268. $new_content = $bbp_shortcodes->display_topics_of_tag( array( 'id' => bbp_get_topic_tag_id() ) ); 
  269.  
  270. // Edit topic tag 
  271. } elseif ( bbp_is_topic_tag_edit() ) { 
  272. $new_content = $bbp_shortcodes->display_topic_tag_form(); 
  273.  
  274. // Reset the post with our new title 
  275. 'ID' => 0,  
  276. 'post_author' => 0,  
  277. 'post_date' => 0,  
  278. 'post_content' => $new_content,  
  279. 'post_type' => '',  
  280. 'post_title' => sprintf( __( 'Topic Tag: %s', bbpress ), '<span>' . bbp_get_topic_tag_name() . '</span>' ),  
  281. 'post_status' => bbp_get_public_status_id(),  
  282. 'comment_status' => 'closed' 
  283. ) ); 
  284.  
  285. /** 
  286. * Bail if the template already matches a bbPress template. This includes 
  287. * archive-* and single-* WordPress post_type matches (allowing 
  288. * themes to use the expected format) as well as all bbPress-specific 
  289. * template files for users, topics, forums, etc... 
  290. * We do this after the above checks to prevent incorrect 404 body classes 
  291. * and header statuses, as well as to set the post global as needed. 
  292. * @see http://bbpress.trac.wordpress.org/ticket/1478/ 
  293. */ 
  294. return $template; 
  295.  
  296. /** 
  297. * If we are relying on bbPress's built in theme compatibility to load 
  298. * the proper content, we need to intercept the_content, replace the 
  299. * output, and display ours instead. 
  300. * To do this, we first remove all filters from 'the_content' and hook 
  301. * our own function into it, which runs a series of checks to determine 
  302. * the context, and then uses the built in shortcodes to output the 
  303. * correct results from inside an output buffer. 
  304. * Uses bbp_get_theme_compat_templates() to provide fall-backs that 
  305. * should be coded without superfluous mark-up and logic (prev/next 
  306. * navigation, comments, date/time, etc...) 
  307. * Hook into the 'bbp_get_bbpress_template' to override the array of 
  308. * possible templates, or 'bbp_bbpress_template' to override the result. 
  309. */ 
  310. } elseif ( bbp_is_theme_compat_active() ) { 
  311. bbp_remove_all_filters( 'the_content' ); 
  312.  
  313.  
  314. return apply_filters( 'bbp_template_include_theme_compat', $template );