Advertisement
nonchiedercilaparola

theme-metaboxes.php

Jan 28th, 2014
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.70 KB | None | 0 0
  1. <?php
  2. /* Option */
  3. $optlayout = array(
  4.     'default' => 'Default',
  5.     'one-col' => 'One Column',
  6.     'two-col-left' => 'Two Column Left',
  7.     'two-col-right' => 'Two Column Right'
  8. );
  9.  
  10. $optarrange = array(
  11.     'ASC' => 'Ascending',
  12.     'DESC' => 'Descending'
  13. );
  14.  
  15. $imagepath =  get_template_directory_uri() . '/images/';
  16. $optlayoutimg = array(
  17.     'default' => $imagepath.'mb-default.png',
  18.     'one-col' => $imagepath.'mb-1c.png',
  19.     'two-col-left' => $imagepath.'mb-2cl.png',
  20.     'two-col-right' => $imagepath.'mb-2cr.png'
  21. );
  22. // Create meta box slider
  23. global $meta_boxes;
  24.  
  25. $meta_boxes[] = array(
  26.     'id' => 'postlayout-option-meta-box',
  27.     'title' => __('Post Options','klasik'),
  28.     'page' => 'post',
  29.     'showbox' => 'meta_option_show_box',
  30.     'context' => 'normal',
  31.     'priority' => 'high',
  32.     'fields' => array(
  33.         array(
  34.             'name' => __('Layout','klasik'),
  35.             'desc' => '<em>'.__('Select the layout you want on this specific post/page. Overrides default site layout.','klasik').'</em>',
  36.             'options' => $optlayoutimg,
  37.             'id' => 'klasik_layout',
  38.             'type' => 'selectimage',
  39.             'std' => ''
  40.         ),
  41.         array(
  42.             'name' => __('Feature on Homepage Slider','klasik'),
  43.             'desc' => '<em>'.__('Checked this checkbox if you want to show this post as Homepage Slider.','klasik').'</em>',
  44.             'id' => 'klasik_slider_post',
  45.             'type' => 'checkbox',
  46.             'std' => ''
  47.         ),
  48.         array(
  49.             'name' => __('Disable Blog Info on Single','klasik'),
  50.             'desc' => '<em>'.__('Checked this checkbox if you want to disable the meta and author information on single page (date, author, categories, etc).','klasik').'</em>',
  51.             'id' => 'klasik_disable_meta',
  52.             'type' => 'checkbox',
  53.             'std' => ''
  54.         )
  55.     )
  56. );
  57.  
  58. $meta_boxes[] = array(
  59.     'id' => 'pagelayout-option-meta-box',
  60.     'title' => __('Layout Settings','klasik'),
  61.     'page' => 'page',
  62.     'showbox' => 'meta_option_show_box',
  63.     'context' => 'normal',
  64.     'priority' => 'high',
  65.     'fields' => array(
  66.         array(
  67.             'name' => __('Layout','klasik'),
  68.             'desc' => '<em>'.__('Select the layout you want on this specific post/page. Overrides default site layout.','klasik').'</em>',
  69.             'options' => $optlayoutimg,
  70.             'id' => 'klasik_layout',
  71.             'type' => 'selectimage',
  72.             'std' => ''
  73.         )
  74.     )
  75. );
  76.  
  77. add_action('add_meta_boxes', 'mytheme_add_box');
  78.  
  79. // Add meta box
  80. function mytheme_add_box() {
  81.     global $meta_boxes;
  82.     foreach($meta_boxes as $meta_box){
  83.         $metaargs = array(
  84.             'meta_array' => $meta_box
  85.         );
  86.         add_meta_box($meta_box['id'], $meta_box['title'], $meta_box['showbox'], $meta_box['page'], $meta_box['context'], $meta_box['priority'], $metaargs);
  87.     }
  88. }
  89.  
  90. function meta_option_show_box($post, $metaargs) {
  91.     global $meta_boxes;
  92.    
  93.     $meta_array = $metaargs['args']['meta_array'];
  94.     // Use nonce for verification
  95.     echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  96.     echo mytheme_create_metabox($meta_array);
  97. }
  98.  
  99.  
  100. // Create Metabox Form Table
  101. function mytheme_create_metabox($meta_box){
  102.  
  103.     global $post;
  104.    
  105.     $returnstring = "";
  106.     $returnstring .='
  107.                     <style type="text/css">
  108.                         .optionimg{border:3px solid #cecece; margin-right:4px;cursor:pointer;}
  109.                         .optionimg.optselected{border-color:#ababab;}
  110.                         .form-table td em{font-style:normal;color:#999999;font-size:11px;}
  111.                     </style>
  112.                     <script type="text/javascript">
  113.                     jQuery(document).ready(function(){
  114.                         jQuery( \'.optionimg\').click(function(){
  115.                             jQuery(this).parent().find( \'.optionimg\').removeClass( \'optselected\' );
  116.                             jQuery(this).addClass( \'optselected\' );
  117.                         });
  118.                     });
  119.                     </script>
  120.                 ';
  121.     $returnstring .= '<table class="form-table">';
  122.  
  123.     foreach ($meta_box['fields'] as $field) {
  124.         // get current post meta data
  125.         $meta = get_post_meta($post->ID, $field['id'], true);
  126.  
  127.         $returnstring .= '<tr>'.
  128.                 '<th style="width:20%;border-bottom:1px solid #e4e4e4;padding:15px 0px"><label for="'. $field['id']. '">'.$field['name']. '</label></th>'.
  129.                 '<td style="border-bottom:1px solid #e4e4e4;padding:15px 0px">';
  130.         switch ($field['type']) {
  131.  
  132. //If Text      
  133.             case 'text':
  134.                 $textvalue = $meta ? $meta : $field['std'];
  135.                 $widthinput = "97%";
  136.                 $prefixinput = "";
  137.                 $postfixinput = "";
  138.                 if(isset($field['class'])){
  139.                     if($field['class']=="mini"){
  140.                         $widthinput = "20%";
  141.                     }
  142.                 }
  143.                 if(isset($field['prefix'])){
  144.                     $prefixinput = stripslashes(trim($field['prefix']));
  145.                 }
  146.                 if(isset($field['postfix'])){
  147.                     $postfixinput = stripslashes(trim($field['postfix']));
  148.                 }
  149.                 $returnstring .= $prefixinput.'<input type="text" name="'. $field['id']. '" id="'. $field['id']. '" value="'. htmlspecialchars($textvalue) .'" size="30" style="width:'.$widthinput.'" /> '.$postfixinput.
  150.                     '<br />'.$field['desc'];
  151.                 break;
  152.  
  153.  
  154. //If Text Area         
  155.             case 'textarea':
  156.                 $textvalue = $meta ? $meta : $field['std'];
  157.                 $returnstring .= '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. htmlspecialchars($textvalue) .'</textarea>'.
  158.                     '<br />'.$field['desc'];
  159.                 break;
  160.  
  161. //If Select Combobox           
  162.             case 'select':
  163.                 $optvalue = $meta ? $meta : $field['std'];
  164.                 $returnstring .= '<select name="'. $field['id']. '" id="'. $field['id']. '">';
  165.                 foreach ($field['options'] as $option => $val){
  166.                     $selectedstr = ($optvalue==$option)? 'selected="selected"' : '';
  167.                     $returnstring .= '<option value="'.$option.'" '.$selectedstr.'>'. $val .'</option>';
  168.                 }
  169.                 $returnstring .= '</select>';
  170.                 $returnstring .= '<br />'.$field['desc'];
  171.                 break;
  172.            
  173.             case 'select-blog-category':
  174.                 $optvalue = $meta ? $meta : $field['std'];
  175.                
  176.                 // Pull all the categories into an array
  177.                 $options_categories = array();
  178.                 $options_categories_obj = get_categories();
  179.                 $options_categories["allcategories"] =__('Select The Category','klasik');
  180.                 foreach ($options_categories_obj as $category) {
  181.                     $options_categories[$category->slug] = $category->cat_name;
  182.                 }
  183.                
  184.                 $returnstring .= '<select name="'. $field['id']. '" id="'. $field['id']. '">';
  185.                 foreach ($options_categories as $option => $val){
  186.                     $selectedstr = ($optvalue==$option)? 'selected="selected"' : '';
  187.                     $returnstring .= '<option value="'.$option.'" '.$selectedstr.'>'. $val .'</option>';
  188.                 }
  189.                 $returnstring .= '</select>';
  190.                
  191.                 $returnstring .= '<br />'.$field['desc'];
  192.                 break;
  193.            
  194.             case 'checkbox-blog-categories':
  195.                 $chkvalue = $meta ? $meta : $field['std'];
  196.                 $chkvalue = explode(",",$chkvalue);
  197.                
  198.                 $portcategories = get_categories();
  199.                 foreach($portcategories as $category){
  200.                     $checkedstr="";
  201.                     if(in_array($category->slug,$chkvalue)){
  202.                         $checkedstr = 'checked="checked"';
  203.                     }
  204.                     $returnstring .= '<div style="float:left;width:30%;">';
  205.                     $returnstring .= '<input type="checkbox" value="'. $category->slug .'" name="'. $field['id']. '[\''.$category->slug.'\']" id="'. $field['id']."-". $category->name . '" '.$checkedstr.' />&nbsp;&nbsp;'. $category->name;
  206.                     $returnstring .= '</div>';
  207.                 }
  208.                 $returnstring .= '<div style="clear:both;"></div><br />'.$field['desc'];
  209.                 break;
  210.  
  211. //If Select Slider Combobox        
  212.             case 'select-slider-category':
  213.                 $optvalue = $meta ? $meta : $field['std'];
  214.                
  215.                 // Pull all the slider categories into an array
  216.                 $options_pcategory = array();
  217.                 $options_pcategory_obj = get_terms('slider-category');
  218.                 $options_categories[""] =__('Select Category','klasik');
  219.                 if($options_pcategory_obj){
  220.                     foreach ($options_pcategory_obj as $pcategory) {
  221.                         $options_pcategory[$pcategory->slug] = $pcategory->name;
  222.                     }
  223.                 }
  224.                
  225.                 $returnstring .= '<select name="'. $field['id']. '" id="'. $field['id']. '">';
  226.                 foreach ($options_pcategory as $option => $val){
  227.                     $selectedstr = ($optvalue==$option)? 'selected="selected"' : '';
  228.                     $returnstring .= '<option value="'.$option.'" '.$selectedstr.'>'. $val .'</option>';
  229.                 }
  230.                 $returnstring .= '</select>';
  231.                
  232.                 $returnstring .= '<br />'.$field['desc'];
  233.                 break;
  234.  
  235. //If Select Slider Combobox        
  236.             case 'select-portfolio-category':
  237.                 $optvalue = $meta ? $meta : $field['std'];
  238.                
  239.                 // Pull all the slider categories into an array
  240.                 $options_pcategory = array();
  241.                 $options_pcategory_obj = get_terms('portfolio-category');
  242.                 $options_categories[""] =__('Select Category','klasik');
  243.                 if($options_pcategory_obj){
  244.                     foreach ($options_pcategory_obj as $pcategory) {
  245.                         $options_pcategory[$pcategory->slug] = $pcategory->name;
  246.                     }
  247.                 }
  248.                
  249.                 $returnstring .= '<select name="'. $field['id']. '" id="'. $field['id']. '">';
  250.                 foreach ($options_pcategory as $option => $val){
  251.                     $selectedstr = ($optvalue==$option)? 'selected="selected"' : '';
  252.                     $returnstring .= '<option value="'.$option.'" '.$selectedstr.'>'. $val .'</option>';
  253.                 }
  254.                 $returnstring .= '</select>';
  255.                
  256.                 $returnstring .= '<br />'.$field['desc'];
  257.                 break;
  258.                
  259. //If Checkbox for Portfolio Categories
  260.             case 'checkbox-portfolio-categories':
  261.                 $chkvalue = $meta ? $meta : $field['std'];
  262.                 $chkvalue = explode(",",$chkvalue);
  263.                 $args = array(
  264.                     "type"          => "portfolio",
  265.                     "taxonomy"  => "portfolio-category"
  266.                 );
  267.                 $portcategories = get_categories($args);
  268.                 foreach($portcategories as $category){
  269.                     $checkedstr="";
  270.                     if(in_array($category->slug,$chkvalue)){
  271.                         $checkedstr = 'checked="checked"';
  272.                     }
  273.                     $returnstring .= '<div style="float:left;width:30%;">';
  274.                     $returnstring .= '<input type="checkbox" value="'. $category->slug .'" name="'. $field['id']. '[\''.$category->slug.'\']" id="'. $field['id']."-". $category->name . '" '.$checkedstr.' />&nbsp;&nbsp;'. $category->name;
  275.                     $returnstring .= '</div>';
  276.                 }
  277.                 $returnstring .= '<div style="clear:both;"></div><br />'.$field['desc'];
  278.                 break;
  279.  
  280. //If Select Image          
  281.             case 'selectimage':
  282.                 $optvalue = $meta ? $meta : $field['std'];
  283.  
  284.                 foreach ($field['options'] as $option => $val){
  285.                     $selectedstr = ($optvalue==$option)? 'optselected' : '';
  286.                     $checkedstr = ($optvalue==$option)? 'checked="checked"' : '';
  287.                     $returnstring .= '<img src="'.$val.'" class="optionimg '.$selectedstr.'" onclick="document.getElementById(\''.$field['id'].$option.'\').checked=true" style="display:inline-block;" />';
  288.                     $returnstring .= '<input type="radio" name="'.$field['id'].'" id="'.$field['id'].$option.'" value="'.$option.'" '.$checkedstr.' style="display:none;"/>';
  289.                 }
  290.                 $returnstring .= '<br />'.$field['desc'];
  291.                 break;
  292.  
  293. //If Checkbox          
  294.             case 'checkbox':
  295.                 $chkvalue = $meta ? true : $field['std'];
  296.                 $checkedstr = ($chkvalue)? 'checked="checked"' : '';
  297.                 $returnstring .= '<input type="checkbox" name="'. $field['id']. '" id="'. $field['id']. '" '.$checkedstr.' />';
  298.                 $returnstring .= '<br />'.$field['desc'];
  299.                 break;
  300.                  
  301. //If Button
  302.             case 'button':
  303.                 $buttonvalue = $meta ? $meta : $field['std'] ;
  304.                 $returnstring .= '<input type="button" name="'. $field['id']. '" id="'. $field['id']. '"value="'. $buttonvalue. '" />';
  305.                 $returnstring .= '<br />'.$field['desc'];
  306.                 break;
  307.  
  308.  
  309.                
  310.         }
  311.         $returnstring .=    '</td>'.
  312.                         '</tr>';
  313.     }
  314.  
  315.     $returnstring .= '</table>';
  316.    
  317.     return $returnstring;
  318.  
  319. }//END : mytheme_create_metabox
  320.  
  321.  
  322. add_action('save_post', 'mytheme_save_data');
  323.  
  324.  
  325. // Save data from meta box
  326. function mytheme_save_data($post_id) {
  327.     global $meta_boxes;
  328.  
  329.     // verify nonce
  330.     if(isset($_POST['mytheme_meta_box_nonce'])){
  331.         if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
  332.             return $post_id;
  333.         }
  334.     }
  335.  
  336.     // check autosave
  337.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  338.         return $post_id;
  339.     }
  340.  
  341.     // check permissions
  342.     if ('page' == isset($_POST['post_type'])) {
  343.         if (!current_user_can('edit_page', $post_id)) {
  344.             return $post_id;
  345.         }
  346.     } elseif (!current_user_can('edit_post', $post_id)) {
  347.         return $post_id;
  348.     }
  349.    
  350.     foreach($meta_boxes as $meta_box){
  351.         foreach ($meta_box['fields'] as $field) {
  352.             $old = get_post_meta($post_id, $field['id'], true);
  353.             $new = (isset($_POST[$field['id']]))? $_POST[$field['id']] : "";
  354.            
  355.            
  356.             if($field['type']=='checkbox-portfolio-categories' || $field['type']=='checkbox-blog-categories'){
  357.                 if(isset($_POST[$field['id']]) && is_array($_POST[$field['id']]) && count($_POST[$field['id']])>0){
  358.                     $values = array_values($_POST[$field['id']]);
  359.                     $valuestring = implode(",",$values);
  360.                     $new = $valuestring;
  361.                    
  362.                 }else{
  363.                     $_POST[$field['id']] = $new = "";
  364.                 }
  365.             }
  366.            
  367.             if($field['type']=='checkbox'){
  368.                 if(!isset($_POST[$field['id']])){
  369.                     $_POST[$field['id']] = $new = false;
  370.                 }
  371.             }
  372.            
  373.             if (isset($_POST[$field['id']]) && $new != $old && (!isset($_POST['_inline_edit']) && !isset($_GET['bulk_edit']))) {
  374.                 update_post_meta($post_id, $field['id'], $new);
  375.             }
  376.         }
  377.     }
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement