Advertisement
Imperative-Ideas

Foundation Reveal Modal Shortcode for WordPress

Jan 6th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.26 KB | None | 0 0
  1. /**
  2.  * Foundation Reveal Shortcode
  3.  * Author : Ian Armstrong (@imperativeideas)
  4.  * Author : G.M (http://wordpress.stackexchange.com/questions/128423/passing-a-shortcode-attribute-to-a-sub-function)
  5.  * Version 1.0
  6.  *
  7.  * Notes: Need to add several features including:
  8.    + Support for manual content entry in place of a page/post slug
  9.    + Support for hiding the close box with a shortcode parameter
  10.    + Support for hiding/changing the background with a shortcode parameter
  11.    + Error trapping if the same slug is entered multiple times
  12.    + Tie this into the TinyMCE Editor as a button
  13.    + Convert this whole thing to a class
  14.  */
  15.  
  16. function reveal_setup($atts = array(), $content = '') {
  17.  
  18.     // Setup the static variable. Use an array to allow multiple calls per page
  19.     static $the_modals = array();
  20.  
  21.     // if the function is called from wp_footer hook
  22.     if ( current_filter() === 'wp_footer' ) {
  23.         if ( is_array($the_modals) && ! empty($the_modals) ) {
  24.             foreach( $the_modals as $amodal ) {
  25.                 echo $amodal;
  26.             }
  27.         }
  28.  
  29.     // if the function is called from shortcode
  30.     } else {
  31.         // Get the attributes (extract is bad, let's not use it)
  32.         $atts = shortcode_atts(
  33.             array( 'size' => 'medium', 'slug' => 'dummy' ),
  34.             $atts,
  35.             'reveal' // enable filtering
  36.         );
  37.  
  38.         // prepare the_modal link
  39.         $modal_link  = '<a href="#" data-reveal-id="' . $atts['slug'] . '">';
  40.         $modal_link .= $content;
  41.         $modal_link .= '</a>';
  42.  
  43.         // prepare the_modal content
  44.         $modal_format = '<div id="%s" class="%s reveal-modal" data-reveal>';
  45.  
  46.         $the_modal = sprintf( $modal_format, $atts['slug'], $atts['size'] );
  47.  
  48.         if ( $atts['slug'] == 'dummy' ) {
  49.             $the_modal .= 'You haven\'t called a page to be revealed in this modal! Remember to include a page slug in your shortcode call:';
  50.             $the_modal .= '<br /><br />';
  51.             $the_modal .= '[reveal slug="your-page-slug"]Your Link Text[/reveal]';
  52.             $the_modal .= '<a class="close-reveal-modal">&#215;</a>';
  53.         } else {
  54.             wp_reset_postdata(); wp_reset_query();
  55.             $modal_query = new WP_Query( array(
  56.                 'name'      =>  $atts['slug'],
  57.                 'post_type' =>  'any'
  58.             ));
  59.             while ($modal_query->have_posts()) : $modal_query->the_post();
  60.                 $modal_content = apply_filters('the_content', get_the_content());
  61.                 $modal_content = str_replace(']]>', ']]&gt;', $modal_content);
  62.                 $the_modal .= $modal_content;
  63.                 $the_modal .= '<a class="close-reveal-modal">&#215;</a>';
  64.             endwhile;
  65.             wp_reset_postdata(); wp_reset_query();
  66.         }
  67.  
  68.         $the_modal .= '</div>';
  69.  
  70.         // save the modal content in the static modals array
  71.         $the_modals[] = $the_modal; // --------> This isn't working as expected <--------
  72.  
  73.         // add the present function to wp_footer hook if it is not already added
  74.         if ( ! has_action('wp_footer', __FUNCTION__) ) {
  75.             add_action( 'wp_footer', __FUNCTION__ );
  76.         }
  77.         // return the modal link
  78.         return $modal_link;
  79.     }
  80. }
  81.  
  82. add_shortcode('reveal', 'reveal_setup');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement