jamesabruce

Wordpress Products Database Plugin, Complete Code

Jul 16th, 2011
2,961
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: Product Database
  4. Description: Versatile product database
  5. Version: 1.0.0
  6. Author: James Bruce
  7. Author URI: http://make-money-blogging-ideas.com/
  8.  */
  9.  
  10. if (!class_exists("Products")) {
  11.     class Products {
  12.    
  13.         function Products(){
  14.             add_action('init', array(&$this,'products_init'));
  15.             register_activation_hook(__FILE__, 'my_rewrite_flush');
  16.         }
  17.    
  18.         // declare the new post type 'sites'
  19.         function products_init() {
  20.             $args = array(
  21.                 'labels' => array(
  22.                     'name' => __('Products'),
  23.                     'singular_name' => __('Product'),
  24.                 ),
  25.                 'public' => true,
  26.                 'rewrite' => array("slug" => "products"), // permalink structure
  27.                 'supports' => array('thumbnail','custom-fields','title','editor','comments'),
  28.                 'has_archive' => true
  29.             );
  30.  
  31.             register_post_type( 'products' , $args );
  32.  
  33.         }
  34.        
  35.         function my_rewrite_flush() {
  36.           products_init();
  37.           flush_rewrite_rules();
  38.         }
  39.        
  40.                
  41.        
  42.     }
  43.    
  44.     class LatestProductsWidget extends WP_Widget
  45.     {
  46.       function LatestProductsWidget()
  47.       {
  48.         $widget_ops = array('classname' => 'LatestProductsWidget', 'description' => 'Displays the latest product additions with thumbnail' );
  49.         $this->WP_Widget('LatestProductsWidget', 'Latest Products and Thumbnail', $widget_ops);
  50.       }
  51.      
  52.       function form($instance)
  53.       {
  54.         $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
  55.         $title = $instance['title'];
  56.     ?>
  57.       <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
  58.     <?php
  59.       }
  60.      
  61.       function update($new_instance, $old_instance)
  62.       {
  63.         $instance = $old_instance;
  64.         $instance['title'] = $new_instance['title'];
  65.         return $instance;
  66.       }
  67.      
  68.       function widget($args, $instance)
  69.       {
  70.         extract($args, EXTR_SKIP);
  71.      
  72.         echo $before_widget;
  73.         $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
  74.      
  75.         if (!empty($title))
  76.           echo $before_title . $title . $after_title;;
  77.      
  78.         // WIDGET CODE GOES HERE
  79.         query_posts('post_type=products&posts_per_page=2&order=DESC&meta_key=Level&orderby=meta_value');
  80.         if (have_posts()) :
  81.             while (have_posts()) : the_post();
  82.                 echo "<a href='".get_permalink()."'>";
  83.                 the_title('<h2>','</h2>');
  84.                 the_post_thumbnail('thumbnail');
  85.                 the_meta();
  86.                 echo "</a>";
  87.             endwhile;
  88.         endif;
  89.         wp_reset_query();
  90.      
  91.         echo $after_widget;
  92.       }
  93.  
  94.     }
  95.     add_action( 'widgets_init', create_function('', 'return register_widget("LatestProductsWidget");') );
  96.        
  97. }
  98.  
  99.  
  100. if (!isset($pd_plugin_instance)) $pd_plugin_instance = new Products();
  101.  
  102.  
  103.  
  104.  
  105. ?>
RAW Paste Data