Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Foundation Reveal Shortcode
- * Author : Ian Armstrong (@imperativeideas)
- * Author : G.M (http://wordpress.stackexchange.com/questions/128423/passing-a-shortcode-attribute-to-a-sub-function)
- * Version 1.0
- *
- * Notes: Need to add several features including:
- + Support for manual content entry in place of a page/post slug
- + Support for hiding the close box with a shortcode parameter
- + Support for hiding/changing the background with a shortcode parameter
- + Error trapping if the same slug is entered multiple times
- + Tie this into the TinyMCE Editor as a button
- + Convert this whole thing to a class
- */
- function reveal_setup($atts = array(), $content = '') {
- // Setup the static variable. Use an array to allow multiple calls per page
- static $the_modals = array();
- // if the function is called from wp_footer hook
- if ( current_filter() === 'wp_footer' ) {
- if ( is_array($the_modals) && ! empty($the_modals) ) {
- foreach( $the_modals as $amodal ) {
- echo $amodal;
- }
- }
- // if the function is called from shortcode
- } else {
- // Get the attributes (extract is bad, let's not use it)
- $atts = shortcode_atts(
- array( 'size' => 'medium', 'slug' => 'dummy' ),
- $atts,
- 'reveal' // enable filtering
- );
- // prepare the_modal link
- $modal_link = '<a href="#" data-reveal-id="' . $atts['slug'] . '">';
- $modal_link .= $content;
- $modal_link .= '</a>';
- // prepare the_modal content
- $modal_format = '<div id="%s" class="%s reveal-modal" data-reveal>';
- $the_modal = sprintf( $modal_format, $atts['slug'], $atts['size'] );
- if ( $atts['slug'] == 'dummy' ) {
- $the_modal .= 'You haven\'t called a page to be revealed in this modal! Remember to include a page slug in your shortcode call:';
- $the_modal .= '<br /><br />';
- $the_modal .= '[reveal slug="your-page-slug"]Your Link Text[/reveal]';
- $the_modal .= '<a class="close-reveal-modal">×</a>';
- } else {
- wp_reset_postdata(); wp_reset_query();
- $modal_query = new WP_Query( array(
- 'name' => $atts['slug'],
- 'post_type' => 'any'
- ));
- while ($modal_query->have_posts()) : $modal_query->the_post();
- $modal_content = apply_filters('the_content', get_the_content());
- $modal_content = str_replace(']]>', ']]>', $modal_content);
- $the_modal .= $modal_content;
- $the_modal .= '<a class="close-reveal-modal">×</a>';
- endwhile;
- wp_reset_postdata(); wp_reset_query();
- }
- $the_modal .= '</div>';
- // save the modal content in the static modals array
- $the_modals[] = $the_modal; // --------> This isn't working as expected <--------
- // add the present function to wp_footer hook if it is not already added
- if ( ! has_action('wp_footer', __FUNCTION__) ) {
- add_action( 'wp_footer', __FUNCTION__ );
- }
- // return the modal link
- return $modal_link;
- }
- }
- add_shortcode('reveal', 'reveal_setup');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement