Advertisement
Guest User

tabs shortcode

a guest
May 14th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Tabs Shortcode
  4. Plugin URI: http://wordpress.org/extend/plugins/tabs-shortcode/
  5. Description: Create shortcode that enables you to create tabs on your pages and posts
  6. Author: CTLT
  7. Version: 1.0.2
  8. Author URI: http://ctlt.ubc.ca
  9. */
  10. global $olt_tab_shortcode_count, $olt_tab_shortcode_tabs;
  11. $olt_tab_shortcode_count = 0;
  12. function olt_display_shortcode_tab($atts,$content)
  13. {
  14. global $olt_tab_shortcode_count, $post, $olt_tab_shortcode_tabs;
  15. extract(shortcode_atts(array(
  16. 'title' => null,
  17. 'class' => null,
  18. ), $atts));
  19.  
  20. ob_start();
  21.  
  22. if($title):
  23. $olt_tab_shortcode_tabs[] = array(
  24. "title" => $title,
  25. "id" => ereg_replace("[^A-Za-z0-9]", "", $title)."-".$olt_tab_shortcode_count,
  26. "class" => $class
  27. );
  28. ?><div id="<?php echo ereg_replace("[^A-Za-z0-9]", "", $title)."-".$olt_tab_shortcode_count; ?>" >
  29. <?php echo do_shortcode( $content ); ?>
  30. </div><?php
  31. elseif($post->post_title):
  32. $olt_tab_shortcode_tabs[] = array(
  33. "title" => $post->post_title,
  34. "id" => ereg_replace("[^A-Za-z0-9]", "", $post->post_title)."-".$olt_tab_shortcode_count,
  35. "class" =>$class
  36. );
  37. ?><div id="<?php echo ereg_replace("[^A-Za-z0-9]", "", $post->post_title)."-".$olt_tab_shortcode_count; ?>" >
  38. <?php echo do_shortcode( $content ); ?>
  39. </div><?php
  40. else:
  41. ?>
  42. <span style="color:red">Please enter a title attribute like [tab title="title name"]tab content[tab]</span>
  43. <?php
  44. endif;
  45. $olt_tab_shortcode_count++;
  46. return ob_get_clean();
  47. }
  48.  
  49. function olt_display_shortcode_tabs( $attr, $content )
  50. {
  51. // wordpress function
  52.  
  53. global $olt_tab_shortcode_count,$post, $olt_tab_shortcode_tabs;
  54. $vertical_tabs = "";
  55. if( isset( $attr['vertical_tabs']) ):
  56. $vertical_tabs = ( (bool)$attr['vertical_tabs'] ? "vertical-tabs": "");
  57. unset($attr['vertical_tabs']);
  58. endif;
  59.  
  60. // $attr['disabled'] = (bool)$attr['disabled'];
  61. $attr['collapsible'] = (bool)$attr['collapsible'];
  62. $query_atts = shortcode_atts(
  63. array(
  64. 'collapsible' => false,
  65. 'event' =>'click',
  66. ), $attr);
  67. // there might be a better way of doing this
  68. $id = "random-tab-id-".rand(0,1000);
  69.  
  70. ob_start();
  71. ?>
  72. <div id="<?php echo $id ?>" class="tabs-shortcode <?php echo $vertical_tabs; ?>"><?php
  73.  
  74. $content = (substr($content,0,6) =="<br />" ? substr( $content,6 ): $content);
  75. $content = str_replace("]<br />","]",$content);
  76.  
  77. $str = do_shortcode( $content ); ?>
  78. <ul>
  79. <?php
  80. foreach( $olt_tab_shortcode_tabs as $li_tab ):
  81.  
  82. ?><li <?php if( $li_tab['class']): ?> class='<?php echo $li_tab['class'];?>' <?php endif; ?> ><a href="#<?php echo $li_tab['id']; ?>"><?php echo $li_tab['title']; ?></a></li><?php
  83. endforeach;
  84.  
  85.  
  86.  
  87. ?></ul><?php echo $str; ?></div>
  88. <script type="text/javascript"> /* <![CDATA[ */
  89. jQuery(document).ready(function($) { $("#<?php echo $id ?>").tabs(<?php echo json_encode($query_atts); ?> ); });
  90. /* ]]> */
  91. </script>
  92.  
  93. <?php
  94. $post_content = ob_get_clean();
  95.  
  96. wp_enqueue_script('jquery');
  97. wp_enqueue_script('jquery-ui-core');
  98. wp_enqueue_script('jquery-ui-tabs');
  99. return str_replace("\r\n", '',$post_content);
  100. }
  101.  
  102.  
  103. function olt_tabs_shortcode_init() {
  104.  
  105. add_shortcode('tab', 'olt_display_shortcode_tab'); // Individual tab
  106. add_shortcode('tabs', 'olt_display_shortcode_tabs'); // The shell
  107.  
  108. // Move wpautop to priority 12 (after do_shortcode)
  109. /*
  110. remove_filter('the_content','wpautop', 10);
  111. add_filter('the_content','wpautop', 12);
  112. */
  113. }
  114.  
  115. add_action('init','olt_tabs_shortcode_init');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement