Advertisement
alchymyth

magazine-columns.php edited for archives etc

Nov 21st, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.69 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Magazine Columns
  4. Plugin URI: http://bavotasan.com/downloads/magazine-columns-wordpress-plugin/
  5. Description: Divides your post or page content into two or more columns, like a magazine article.
  6. Author: c.bavota
  7. Version: 1.0.4
  8. edited 21/11/2011 to work in multipost pages such as index and archives
  9. Author URI: http://www.bavotasan.com/
  10. */
  11.  
  12. // Replace the_content function if a <!--column--> tag is detected
  13. function add_columns($content) {
  14.     if(stristr($content, '<!--column-->') ) {
  15.     $return_value = ''; //the variable for the columnated content
  16.         if(stristr($content, '<!--startcolumns-->')) {
  17.             $topcontent = explode('<!--startcolumns-->', $content);
  18.             if(stristr($content, '<!--stopcolumns-->')) {
  19.                 $bottomcontent = explode('<!--stopcolumns-->', $topcontent[1]);
  20.                 $content = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$bottomcontent[0]);
  21.             } else {
  22.                 $content = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$topcontent[1]);
  23.             }
  24.         } else {
  25.             $content = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$content);      
  26.         }
  27.         $content = explode('<!--column-->', $content);
  28.         $count = count($content);
  29.         if($count == 2) {
  30.             $colname = "col_two";
  31.         } elseif($count == 3) {
  32.             $colname = "col_three";
  33.         } elseif($count == 4) {
  34.             $colname = "col_four";
  35.         } else {
  36.             $colname = "col_five";
  37.         }      
  38.         $x = 1;
  39.         if(!empty($topcontent[0])) {
  40.             $top = explode('<br />',$topcontent[0]);
  41.             $i = count($top);
  42.             $top[$i-1] .= '</p>'."\n";
  43.             $top = implode("", $top);
  44.             $return_value .= $top;
  45.         }      
  46.         foreach($content as $column) {
  47.             $output = "\n".'<div class="columns '.$colname.$x.'">'.$column.'</div>'; //edited to css classes
  48.             $output = str_replace('<div class="columns '.$colname.$x.'"><br />','<div class="columns '.$colname.$x.'"><p>', $output);
  49.             if($x == 5) {
  50.                 unset($content[0]);
  51.                 unset($content[1]);
  52.                 unset($content[2]);
  53.                 unset($content[3]);
  54.                 $column = implode("", $content);
  55.                 $return_value .= $output;
  56.                 break;
  57.             } else {
  58.             $return_value .= $output;
  59.             }
  60.             $x++;
  61.         }
  62.         if(!empty($bottomcontent[1])) {
  63.             $bottom = explode('<br />',$bottomcontent[1]);
  64.             $bottom[0] = '<p style="clear: both;">' . $bottom[0];
  65.             $bottom = implode("", $bottom);
  66.             $return_value .= $bottom;
  67.         }
  68.         return force_balance_tags($return_value); //repair some of the invalid codes
  69.     } else {
  70.         return $content;
  71.     }
  72. }
  73.  
  74. // Adds the add_columns() function to the_content()
  75. add_filter('the_content', 'add_columns');
  76.  
  77. // Creates the CSS for columns
  78. // edited to use css classes and to always output all possible classes
  79. function add_columns_css() {
  80.     global $post;
  81.     $content = $post->post_content;
  82.     if(stristr($content, '<!--column-->') ) {
  83.         $content = explode('<!--column-->', $content);
  84.         $count = count($content);  
  85.    
  86.         $two = 47; // width of two columns
  87.         $three = 29.5; // width of three columns
  88.         $four = 21; // width of four columns
  89.         $five = 15; // width of five columns
  90.         echo '<!-- Magazine Columns CSS -->'."\n";
  91.         echo "<style type='text/css'>\n";
  92.         if($count >=1) {
  93.         echo " .col_two1 { float: left; width: ".$two."%; }\n";
  94.         echo " .col_two2 { float: left; width: ".$two."%; margin: 0 0 0 5%; }\n";
  95.         }
  96.         if($count >=1) {   
  97.         echo " .col_three1, #col_three3 { float: left; width: ".$three."%; }\n";
  98.         echo " .col_three2 { float: left; width: ".$three."%; margin: 0 5%; }\n";
  99.         }
  100.         if($count >=1) {
  101.         echo " .col_four1, #col_four4 { float: left; width: ".$four."%; }\n";
  102.         echo " .col_four2 { float: left; width: ".$four."%; margin: 0 2.5% 0 5%; }\n";
  103.         echo " .col_four3 { float: left; width: ".$four."%; margin: 0 5% 0 2.5%; }\n";
  104.         }
  105.         if($count >=1) {
  106.         echo " .col_five1, #col_five5 { float: left; width: ".$five."%; }\n";
  107.         echo " .col_five2 { float: left; width: ".$five."%; margin: 0 2.5% 0 5%; }\n";
  108.         echo " .col_five3 { float: left; width: ".$five."%; margin: 0 2.5% 0 5%; }\n";
  109.         echo " .col_five4 { float: left; width: ".$five."%; margin: 0 5% 0 2.5%; }\n";
  110.         }
  111.         echo " .columns img { width: 98% }\n";
  112.         echo "</style>\n";
  113.         echo '<!-- eof Magazine Columns CSS -->'."\n";
  114.     }
  115. }
  116. add_action('wp_head', 'add_columns_css');
  117.  
  118. // Creates the columns quicktag button
  119. function columns_quicktag_button() {
  120.   if (strpos($_SERVER['REQUEST_URI'], 'post.php') ||
  121.       strpos($_SERVER['REQUEST_URI'], 'post-new.php') ||
  122.       strpos($_SERVER['REQUEST_URI'], 'page.php') ||
  123.       strpos($_SERVER['REQUEST_URI'], 'page-new.php')) {
  124.     echo '<script type="text/javascript" src="'  . get_option('siteurl') . '/wp-content/plugins/magazine-columns/js/mc.js"></script>';
  125.     }
  126. }
  127.  
  128. //Add the columns_quicktag_button() function to the proper admin pages
  129. add_filter('admin_footer', 'columns_quicktag_button');
  130. ?>
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement