do_accordion_sections

Meta Box Accordion Template Function.

Description

(int) do_accordion_sections( (string|object) $screen, (string) $context, (mixed) $object ); 

Largely made up of abstracted code from do_meta_boxes(), this function serves to build meta boxes as list items for display as a collapsible accordion.

Returns (int)

number of meta boxes as accordion sections.

Parameters (3)

0. $screen (string|object)
The screen identifier.
1. $context (string)
The meta box context.
2. $object (mixed)
Gets passed to the section callback function as first parameter.

Usage

  1. if ( !function_exists( 'do_accordion_sections' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/template.php'; 
  3.  
  4. // The screen identifier. 
  5. $screen = null; 
  6.  
  7. // The meta box context. 
  8. $context = ''; 
  9.  
  10. // gets passed to the section callback function as first parameter. 
  11. $object = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = do_accordion_sections($screen, $context, $object); 
  15.  

Defined (1)

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

/wp-admin/includes/template.php  
  1. function do_accordion_sections( $screen, $context, $object ) { 
  2. global $wp_meta_boxes; 
  3.  
  4. wp_enqueue_script( 'accordion' ); 
  5.  
  6. if ( empty( $screen ) ) 
  7. $screen = get_current_screen(); 
  8. elseif ( is_string( $screen ) ) 
  9. $screen = convert_to_screen( $screen ); 
  10.  
  11. $page = $screen->id; 
  12.  
  13. $hidden = get_hidden_meta_boxes( $screen ); 
  14. ?> 
  15. <div id="side-sortables" class="accordion-container"> 
  16. <ul class="outer-border"> 
  17. <?php 
  18. $i = 0; 
  19. $first_open = false; 
  20.  
  21. if ( isset( $wp_meta_boxes[ $page ][ $context ] ) ) { 
  22. foreach ( array( 'high', 'core', 'default', 'low' ) as $priority ) { 
  23. if ( isset( $wp_meta_boxes[ $page ][ $context ][ $priority ] ) ) { 
  24. foreach ( $wp_meta_boxes[ $page ][ $context ][ $priority ] as $box ) { 
  25. if ( false == $box || ! $box['title'] ) 
  26. continue; 
  27. $i++; 
  28. $hidden_class = in_array( $box['id'], $hidden ) ? 'hide-if-js' : ''; 
  29.  
  30. $open_class = ''; 
  31. if ( ! $first_open && empty( $hidden_class ) ) { 
  32. $first_open = true; 
  33. $open_class = 'open'; 
  34. ?> 
  35. <li class="control-section accordion-section <?php echo $hidden_class; ?> <?php echo $open_class; ?> <?php echo esc_attr( $box['id'] ); ?>" id="<?php echo esc_attr( $box['id'] ); ?>"> 
  36. <h3 class="accordion-section-title hndle" tabindex="0"> 
  37. <?php echo esc_html( $box['title'] ); ?> 
  38. <span class="screen-reader-text"><?php _e( 'Press return or enter to open this section' ); ?></span> 
  39. </h3> 
  40. <div class="accordion-section-content <?php postbox_classes( $box['id'], $page ); ?>"> 
  41. <div class="inside"> 
  42. <?php call_user_func( $box['callback'], $object, $box ); ?> 
  43. </div><!-- .inside --> 
  44. </div><!-- .accordion-section-content --> 
  45. </li><!-- .accordion-section --> 
  46. <?php 
  47. ?> 
  48. </ul><!-- .outer-border --> 
  49. </div><!-- .accordion-container --> 
  50. <?php 
  51. return $i;