Advertisement
Guest User

custom post types

a guest
May 4th, 2010
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.21 KB | None | 0 0
  1. <?php
  2. class DCMerch {
  3.                    
  4.     function DCMerch() {       
  5.         add_action("init", array(&$this, "merch_init"));
  6.         add_action("admin_init", array(&$this, "merch_admin_init"));
  7.     }
  8.    
  9.     function merch_init()
  10.     {
  11.         register_post_type('Merch', array(
  12.             'label' => 'Merch',
  13.             'public' => true,
  14.             'show_ui' => true,
  15.             '_builtin' => false,
  16.             'capability_type' => 'post',
  17.             'hierarchical' => false,
  18.             'menu_position' => 5,
  19.             'rewrite' => array('slug' => 'merch'),
  20.             'supports' => array('title')
  21.         ));
  22.        
  23.         register_taxonomy('client', 'Merch',
  24.             array(
  25.                  'hierarchical' => false,
  26.                  'label' => 'Clients',
  27.                  'singular_label' => 'Client',
  28.                  'query_var' => 'client',
  29.                  'show_ui' => false,
  30.                  'rewrite' => array('slug' => 'client' )
  31.             )
  32.         );
  33.            
  34.         add_action("template_redirect", 'merch_template_redirect');
  35.     }
  36.    
  37.     function merch_admin_init()
  38.     {  
  39.         add_filter("manage_edit-Merch_columns", array(&$this, "edit_merch_columns"));
  40.         add_action("manage_posts_custom_column", array(&$this, "custom_columns"));
  41.        
  42.         // Custom meta boxes for the edit merch screen
  43.         add_meta_box("merch-meta", "Merch Options", array(&$this, "merch_meta_options"), "Merch", "advanced", "high");
  44.         add_action('save_post','save_merch_meta');
  45.     }  
  46.    
  47.     //Defines the columns for the merch post type
  48.     function edit_merch_columns($columns)
  49.     {
  50.         $columns = array(
  51.             "cb" => "<input type=\"checkbox\" />",
  52.             "title" => "Title",
  53.             "client" => "Client",
  54.             "thumb" => "Thumbnail",
  55.             "date" => "Date"
  56.         );
  57.    
  58.         return $columns;
  59.     }
  60.  
  61.     //Outputs custom columns depending on the type
  62.     function custom_columns($column)
  63.     {
  64.         global $post;
  65.         $meta = get_post_meta($post->ID,'_merch',TRUE);
  66.         $client = wp_get_post_terms($post->ID, 'client');
  67.         if ($client == null || count($client) != 1)
  68.             $client = '';
  69.         else
  70.             $client = $client[0]->name;
  71.         switch ($column)
  72.         {
  73.             case "thumb":
  74.                 echo $meta['imageurl'];
  75.                 break;
  76.             case "client":
  77.                 echo $client;
  78.                 break;
  79.         }
  80.     }          
  81.            
  82.     // Admin post meta contents
  83.     function merch_meta_options()
  84.     {          
  85.         global $post;
  86.  
  87.         $meta = get_post_meta($post->ID,'_merch',TRUE);
  88.         $client = wp_get_post_terms($post->ID, 'client');
  89.         if ($client == null || count($client) != 1)
  90.             $client = '';
  91.         else
  92.             $client = $client[0]->name;
  93.        
  94.         include('merch_meta.php');
  95.        
  96.         echo '<input type="hidden" name="_merch_noncename" value="' . wp_create_nonce(plugin_basename(__FILE__)) . '" />';
  97.     }          
  98. }
  99.  
  100. function merch_template_redirect()
  101. {
  102.     global $wp;
  103.     if (array_key_exists('merch', $wp->query_vars)) {
  104.         include(TEMPLATEPATH . "/merch.php");
  105.         die();
  106.     }  
  107. }
  108.  
  109. function save_merch_meta($post_id)
  110. {    
  111.     // authentication checks
  112.  
  113.     // make sure data came from our meta box
  114.     if (!wp_verify_nonce($_POST['_merch_noncename'],plugin_basename(__FILE__))) return $post_id;
  115.  
  116.     // check user permissions
  117.     if ($_POST['post_type'] == 'page')
  118.     {
  119.         if (!current_user_can('edit_page', $post_id)) return $post_id;
  120.     }
  121.     else
  122.     {
  123.         if (!current_user_can('edit_post', $post_id)) return $post_id;
  124.     }
  125.  
  126.     // authentication passed, save data
  127.  
  128.     $data = get_post_meta($post_id, '_merch', TRUE);   
  129.  
  130.     $new_data = $_POST['_merch'];
  131.  
  132.     foreach ($new_data as $n => $v)
  133.     {
  134.         if ($n == 'client') {
  135.             if (trim($v) != '') {
  136.                 wp_set_post_terms($post_id, trim($v), 'client');
  137.             }
  138.             unset($new_data[$n]);
  139.         }
  140.         else if ($n == 'imagepath') {
  141.             if ($new_data[$n] != $data[$n])
  142.             {
  143.                 $attachment = array(
  144.                     'post_title' => 'file'. time(),
  145.                     'post_content' => '',  
  146.                     'post_type' => 'attachment',
  147.                     'post_parent' => $post_id, 
  148.                     'post_mime_type' => 'image/jpeg'
  149.                 );
  150.  
  151.                 $attach_id = wp_insert_attachment( $attachment, $new_data[$n], $post_id );
  152.                 $attach_data = wp_generate_attachment_metadata( $attach_id, $new_data[$n] );
  153.                 wp_update_attachment_metadata( $attach_id,  $attach_data );
  154.             }
  155.         }
  156.         else
  157.         {
  158.             if (is_array($v))
  159.             {
  160.                 foreach ($new_data[$n] as $nn => $vv)
  161.                 {
  162.                     if (trim($vv) === '') unset($new_data[$n][$nn]);
  163.                 }
  164.             }
  165.             else
  166.             {
  167.                 if (trim($v) === '') unset($new_data[$n]);
  168.             }
  169.         }
  170.     }
  171.  
  172.     if ($data) update_post_meta($post_id, '_merch', $new_data);
  173.     else add_post_meta($post_id, '_merch', $new_data, TRUE);
  174. }
  175.  
  176. new DCMerch();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement