Advertisement
OptimatorX

WP Catalogue - wpc-product.php

Jun 7th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.59 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5. //////// Advance custom post type
  6.  
  7.  
  8.  
  9. function wpt_wpcproduct_posttype() {
  10.  
  11. register_post_type( 'wpcproduct',
  12.  
  13. array(
  14.  
  15. 'labels' => array(
  16.  
  17. 'name' => __( 'WP Catalogue' ),
  18.  
  19. 'singular_name' => __( 'WP Catalogue' ),
  20.  
  21. 'add_new' => __( 'Add New Product' ),
  22.  
  23. 'add_new_item' => __( 'Add New Product' ),
  24.  
  25. 'edit_item' => __( 'Edit Product' ),
  26.  
  27. 'new_item' => __( 'Add New Product' ),
  28.  
  29. 'view_item' => __( 'View Product' ),
  30.  
  31. 'search_items' => __( 'Search WPC Product' ),
  32.  
  33. 'not_found' => __( 'No Product found' ),
  34.  
  35. 'not_found_in_trash' => __( 'No Product found in trash' )
  36.  
  37. ),
  38.  
  39. 'public' => true,
  40.  
  41. 'menu_icon' => WP_CATALOGUE.'/images/shopping-basket.png', // Icon Path
  42.  
  43. 'supports' => array( 'title','editor'),
  44.  
  45. 'capability_type' => 'post',
  46.  
  47. 'rewrite' => array("slug" => "wpcproduct"), // Permalinks format
  48.  
  49. 'menu_position' => 121,
  50.  
  51. 'register_meta_box_cb' => 'add_wpcproduct_metaboxes',
  52.  
  53. )
  54.  
  55. );
  56.  
  57. }
  58.  
  59.  
  60.  
  61. add_action( 'init', 'wpt_wpcproduct_posttype' );
  62.  
  63. add_action( 'add_meta_boxes', 'add_wpcproduct_metaboxes' );
  64.  
  65.  
  66.  
  67. function add_wpcproduct_metaboxes() {
  68.  
  69. add_meta_box('wpt_product_imgs', 'Product Images', 'wpt_product_imgs', 'wpcproduct');
  70.  
  71. add_meta_box('wpt_product_price', 'Product Price', 'wpt_product_price', 'wpcproduct', 'side');
  72.  
  73. }
  74.  
  75.  
  76.  
  77. function wpt_product_price() {
  78.  
  79. global $post;
  80.  
  81. // Noncename needed to verify where the data originated
  82.  
  83. echo '<input type="hidden" name="itemmeta_noncename" id="itemmeta_noncename" value="' .
  84.  
  85. wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
  86.  
  87.  
  88.  
  89. // Get the location data if its already been entered
  90.  
  91.  
  92.  
  93. $product_price = get_post_meta($post->ID, 'product_price', true);
  94.  
  95. // Echo out the field
  96.  
  97. echo '<input type="text" name="product_price" value="'.$product_price.'">';
  98.  
  99.  
  100.  
  101.  
  102.  
  103. }
  104.  
  105. function wpt_product_imgs() {
  106.  
  107. global $post;
  108.  
  109. // Noncename needed to verify where the data originated
  110.  
  111. echo '<input type="hidden" name="itemmeta_noncename" id="itemmeta_noncename" value="' .
  112.  
  113. wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
  114.  
  115.  
  116.  
  117. // Get the location data if its already been entered
  118.  
  119. $product_img1 = get_post_meta($post->ID, 'product_img1', true);
  120.  
  121. $product_img2 = get_post_meta($post->ID, 'product_img2', true);
  122.  
  123. $product_img3 = get_post_meta($post->ID, 'product_img3', true);
  124.  
  125. $product_img4 = get_post_meta($post->ID, 'product_img3', true);
  126.  
  127. $product_img5 = get_post_meta($post->ID, 'product_img5', true);
  128.  
  129.  
  130.  
  131. // Echo out the field
  132.  
  133. echo '<p><label><strong>Image 1</strong></label><input id="Image" class="upload-url" type="text" name="product_img1" value="'.$product_img1.'"><input id="st_upload_button" class="st_upload_button" type="button" name="upload_button" value="Upload"></p>';
  134.  
  135.  
  136.  
  137. echo '<p><label><strong>Image 2</strong></label><input id="Image1" class="upload-url" type="text" name="product_img2" value="'.$product_img2.'"><input id="st_upload_button1" class="st_upload_button" type="button" name="upload_button" value="Upload"></p>';
  138.  
  139.  
  140.  
  141. echo '<p><label><strong>Image 3</strong></label><input id="Image1" class="upload-url" type="text" name="product_img3" value="'.$product_img3.'"><input id="st_upload_button1" class="st_upload_button" type="button" name="upload_button" value="Upload"></p>';
  142.  
  143.  
  144.  
  145. echo '<p><label><strong>Image 4</strong></label><input id="Image1" class="upload-url" type="text" name="product_img4" value="'.$product_img4.'"><input id="st_upload_button1" class="st_upload_button" type="button" name="upload_button" value="Upload"></p>';
  146.  
  147.  
  148.  
  149. echo '<p><label><strong>Image 5</strong></label><input id="Image1" class="upload-url" type="text" name="product_img5" value="'.$product_img5.'"><input id="st_upload_button1" class="st_upload_button" type="button" name="upload_button" value="Upload"></p>';
  150.  
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157. // Save the Metabox Data
  158.  
  159. function wpt_save_wpcproduct_meta($post_id, $post) {
  160.  
  161.  
  162.  
  163. // verify this came from the our screen and with proper authorization,
  164.  
  165. // because save_post can be triggered at other times
  166.  
  167. if ( !wp_verify_nonce( $_POST['itemmeta_noncename'], plugin_basename(__FILE__) )) {
  168.  
  169. return $post->ID;
  170.  
  171. }
  172.  
  173. // Is the user allowed to edit the post or page?
  174.  
  175. if ( !current_user_can( 'edit_post', $post->ID ))
  176.  
  177. return $post->ID;
  178.  
  179. // OK, we're authenticated: we need to find and save the data
  180.  
  181. // We'll put it into an array to make it easier to loop though.
  182.  
  183. $item_meta['product_img1'] = $_POST['product_img1'];
  184.  
  185. $item_meta['product_img2'] = $_POST['product_img2'];
  186.  
  187. $item_meta['product_img3'] = $_POST['product_img3'];
  188.  
  189. $item_meta['product_img4'] = $_POST['product_img4'];
  190.  
  191. $item_meta['product_img5'] = $_POST['product_img5'];
  192.  
  193. $item_meta['product_price'] = $_POST['product_price'];
  194.  
  195. // Add values of $events_meta as custom fields
  196.  
  197. foreach ($item_meta as $key => $value) { // Cycle through the $events_meta array!
  198.  
  199. if( $post->post_type == 'revision' ) return; // Don't store custom data twice
  200.  
  201. $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
  202.  
  203. if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
  204.  
  205. update_post_meta($post->ID, $key, $value);
  206.  
  207. } else { // If the custom field doesn't have a value
  208.  
  209. add_post_meta($post->ID, $key, $value);
  210.  
  211. }
  212.  
  213. if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
  214.  
  215. }
  216.  
  217. }
  218.  
  219.  
  220.  
  221. add_action('save_post', 'wpt_save_wpcproduct_meta', 1, 2); // save the custom fields
  222.  
  223. add_action('init','create_wpcproduct_taxonomies',0);
  224.  
  225. function create_wpcproduct_taxonomies(){
  226.  
  227.  
  228.  
  229.  
  230.  
  231. $labels = array(
  232.  
  233. 'name' => _x( 'Categories', 'taxonomy general name' ),
  234.  
  235. 'singular_name' => _x( 'Categories', 'taxonomy singular name' ),
  236.  
  237. 'search_items' => __( 'Search Categories' ),
  238.  
  239. 'all_items' => __( 'All Categories' ),
  240.  
  241. 'parent_item' => __( 'Parent Categories' ),
  242.  
  243. 'parent_item_colon' => __( 'Parent Categories:' ),
  244.  
  245. 'edit_item' => __( 'Edit Categories' ),
  246.  
  247. 'update_item' => __( 'Update Categories' ),
  248.  
  249. 'add_new_item' => __( 'Add New Categories' ),
  250.  
  251. 'new_item_name' => __( 'New Categories Name' ),
  252.  
  253. 'menu_name' => __( 'Categories' ),
  254.  
  255. );
  256.  
  257. register_taxonomy('wpccategories',array('wpcproduct'), array(
  258.  
  259. 'hierarchical' => true,
  260.  
  261. 'labels' => $labels,
  262.  
  263. 'show_ui' => true,
  264.  
  265. 'query_var' => true,
  266.  
  267. 'rewrite' => array( 'slug' => 'wpccategories', 'with_front' => false ),
  268.  
  269. ));
  270.  
  271. }
  272.  
  273.  
  274.  
  275. add_filter( 'manage_edit-wpcproduct_columns', 'my_edit_wpcproduct_columns' ) ;
  276.  
  277. function my_edit_wpcproduct_columns( $columns ) {
  278.  
  279. $columns = array(
  280.  
  281. 'cb' => '<input type="checkbox" />',
  282.  
  283. 'title' => __( 'Title' ),
  284.  
  285. 'wpccategories' => __( '<a href="javascript:;">Category</a>' ),
  286.  
  287. 'date' => __( 'Date' )
  288.  
  289. );
  290.  
  291. return $columns;
  292.  
  293. }
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301. add_action( 'manage_wpcproduct_posts_custom_column', 'my_manage_wpcproduct_columns', 10, 2 );
  302.  
  303. function my_manage_wpcproduct_columns( $column, $post_id ) {
  304.  
  305. global $post;
  306.  
  307.  
  308.  
  309. switch( $column ) {
  310.  
  311. /* If displaying the 'genre' column. */
  312.  
  313. case 'wpccategories':
  314.  
  315. /* Get the genres for the post. */
  316.  
  317. $terms = get_the_terms( $post_id, 'wpccategories');
  318.  
  319. /* If terms were found. */
  320.  
  321. if ( !empty( $terms ) ) {
  322.  
  323. $out = array();
  324.  
  325. /* Loop through each term, linking to the 'edit posts' page for the specific term. */
  326.  
  327. foreach ( $terms as $term ) {
  328.  
  329. $out[] = sprintf( '<a href="%s">%s</a>',
  330.  
  331. esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'wpccategories' => $term->slug ), 'edit.php' ) ),
  332.  
  333. esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'wpccategories', 'display' ) )
  334.  
  335. );
  336.  
  337. }
  338.  
  339. /* Join the terms, separating them with a comma. */
  340.  
  341. echo join( ', ', $out );
  342.  
  343. }
  344.  
  345. /* If no terms were found, output a default message. */
  346.  
  347. else {
  348.  
  349. _e( 'No Category' );
  350.  
  351. }
  352.  
  353. break;
  354.  
  355. /* Just break out of the switch statement for everything else. */
  356.  
  357. default :
  358.  
  359. break;
  360.  
  361. }
  362.  
  363. }
  364.  
  365.  
  366.  
  367. function dev_check_current_screen() {
  368.  
  369. global $current_screen;
  370.  
  371. if($current_screen->post_type=='wpcproduct'){
  372.  
  373. echo '<style type="text/css">
  374.  
  375. #wp-content-media-buttons{
  376.  
  377. display:none;
  378.  
  379. }
  380.  
  381. </style>';
  382.  
  383. }
  384.  
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement