Using Custom Post Types in Wordpress 3.0

Posted: February 23rd, 2010 | Author: David Scott Tufts | Filed under: PHP, Web Development, Wordpress | Tags: , , , | No Comments »

So what is all of the buzz about custom post types in Wordpress 2.9 and 3.0? What can we do with a “post_type” and why does it matter? For some of us developers out there who have known the secret that Wordpress is actually much more than just a blogging platform this in no big news. To the rest of the developers out there this translates to “Wordpress is now a CMS” or something to that effect.

For some time now Wordpress has been more than just a blogging tool. With a little bit of imagination Wordpress can be used not only as a CMS (Content Management System) but also as a PHP framework on which to launch complex web applications. By simply allowing for developers to register any custom “post_type” Wordpress has made this task a whole lot easier for us.

This blog post will be a work in progress, Wordpress 3.0 is still in development at this time and not due to be launched for another couple of months. As new options become available over the next couple of months I will continue to update this post to reflect the changes being made in the nightly builds.

  • How do you plan on using custom post types in your Wordpress themes and plugins?
  • Will this change the way you currently use categories and tags to differentiate content types?
  • Does this help solidify in your mind that Wordpress is finally a CMS?

Also, make sure to check out what Justin Tadlock has to say on this matter.

  1.  
  2. $custom_post_types = array();
  3.  
  4. add_action(‘init’, ‘init_custom_post_types’);
  5. if(!is_admin())
  6.   add_filter(‘pre_get_posts’, ’set_custom_post_types’);
  7.  
  8. function init_custom_post_types()
  9. {
  10.   global $wpdb, $custom_post_types;
  11.  
  12.   // Create a default custom ‘post_type’ called custom_post_types.
  13.   // This adds a "Post Types" UI panel in the admin and allows us
  14.   // to create new post types on the fly just by adding a new post
  15.   // through the admin interface.
  16.   $post_type_args = array(
  17.     ‘label’=>__(‘Post Types’),
  18.     ‘public’=>true,
  19.     ’show_ui’=>true,
  20.     ‘exclude_from_search’=>true
  21.   );
  22.   register_post_type(‘custom_post_types’, $post_type_args);
  23.  
  24.   // If there are any posts with the ‘post_type’ of custom_post_types
  25.   // their names and titles will be returned and new ‘post_types’ will
  26.   // automatically be registered for each record.
  27.   $sql = "
  28.    SELECT
  29.      {$wpdb->posts}.post_name,
  30.      {$wpdb->posts}.post_title
  31.    FROM
  32.      {$wpdb->posts}
  33.    WHERE
  34.      {$wpdb->posts}.post_type = ‘custom_post_types’
  35.      AND {$wpdb->posts}.post_status = ‘publish’";
  36.  
  37.   foreach($post_types = $wpdb->get_results($sql) as $post_type)
  38.   {
  39.     // Store a global array of custom post types for modifying the
  40.     // search query to include the new post types we just registered
  41.     $custom_post_types[$post_type->post_name] = array(
  42.       ‘name’=>$post_type->post_name,
  43.       ‘title’=>$post_type->post_title
  44.     );
  45.  
  46.     $post_type_args = array(
  47.       ‘taxonomies’=>array(‘post_tag’,‘category’),
  48.       ‘label’=>__((string)$post_type->post_title),
  49.       ‘public’=>true,
  50.       ’show_ui’=>true,
  51.       ‘exclude_from_search’=>false,
  52.       ‘register_meta_box_cb’=>‘init_custom_meta_boxes’,
  53.       ‘_builtin’=>true,
  54.       ’supports’=>array(‘title’,‘editor’,‘thumbnail’,‘excerpts’,‘trackbacks’,‘custom-fields’,‘comments’,‘author’,‘revisions’)
  55.     );
  56.     register_post_type((string)$post_type->post_name, $post_type_args);
  57.   }
  58. }
  59.  
  60. function set_custom_post_types($query)
  61. {
  62.   global $custom_post_types;
  63.  
  64.   if($custom_post_types)
  65.   {
  66.     // Here we create an array of all valid post types
  67.     // that we want to appear on the blog.
  68.     $post_types = array(‘post’, ‘attachment’); //’page’,
  69.     foreach($custom_post_types as $key=>$post_type)
  70.     {
  71.       $post_types[] = $post_type[‘name’];
  72.     }
  73.   }
  74.  
  75.   // Now we modify the query to include the newly registed post types
  76.     $query->set(‘post_type’, $post_types);
  77.     return $query;
  78. }
  79.  
  80. function init_custom_meta_boxes($args)
  81. {
  82.   add_meta_box(’setcustommeta’, __(‘Attributes’), ’set_custom_meta_boxes’, $args->post_type, ‘normal’, ‘core’);
  83. }
  84.  
  85. function set_custom_meta_boxes($post)
  86. {
  87.   // add custom meta boxes
  88. }
  89.  

How To Create Barcodes in PHP

Posted: March 31st, 2009 | Author: David Scott Tufts | Filed under: PHP | Tags: , , , | No Comments »

A few years ago I needed a way to generate barcodes on-the-fly using PHP, at the time there were several solutions available at a steep cost, so I wrote this script that generates barcodes in four barcode formats including Code 128, Code 39, Code 2of5, and Codabar. With a little over 100 lines of code you have the options of “vertical” or “horizontal” display, varying barcode heights, and one of four barcode formats. It does require the GD Library to be installed as a module in PHP.

This code is available for use under the MIT License

View the code here: http://www.davidscotttufts.com/code/barcode.phps


Example 1:

Parameters:
Text: “0″ (Default)
Size: “20″ (Default)
Code Type: “Code128″ (Default)
Orientation: “Horizontal” (Default)

HTML Source Code:

  1. <img src="/code/barcode.php" alt="testing" />

Result:
0


Example 2:

Parameters:
Text: “testing”
Size: “20″ (Default)
Code Type: “Code128″ (Default)
Orientation: “Horizontal” (Default)

HTML Source Code:

  1. <img src="/code/barcode.php?text=testing" alt="testing" />

Result:
testing


Example 3:

Parameters:
Text: “TESTING”
Size: “40″
Code Type: “Code39″
Orientation: “Horizontal” (Default)

HTML Source Code:

  1. <img src="/code/barcode.php?codetype=Code39&size=40&text=TESTING" alt="TESTING" />

Result:
TESTING


Example 4:

Parameters:
Text: “12345″
Size: “40″
Code Type: “Code25″
Orientation: “Horizontal” (Default)

HTML Source Code:

  1. <img src="/code/barcode.php?codetype=Code25&size=40&text=12345" alt="12345" />

Result:
12345


Example 5:

Parameters:
Text: “123ABC”
Size: “40″
Code Type: “Codabar”
Orientation: “Horizontal” (Default)

HTML Source Code:

  1. <img src="/code/barcode.php?codetype=Codabar&size=40&text=123ABC" alt="123ABC" />

Result:
123ABC


Example 6:

Parameters:
Text: “The Real David Tufts”
Size: “40″
Code Type: “Code128″ (Default)
Orientation: “Vertical”

HTML Source Code:

  1. <img src="/code/barcode.php?text=The%20Real%20David%20Tufts&orientation=vertical&size=40" alt="The Real David Tufts" />

Result:
The Real David Tufts


Please leave comments with additions or improvements to this script. One possible area of improvement for Code 128 is figuring out how to add keyboard commands (i.e. SHIFT, DEL, FNC 1, etc.) to the barcode.

Again, you can view the code here: http://www.davidscotttufts.com/code/barcode.phps