Categories
PHP Programming Wordpress Development

WordPress 3 Custom Post Type Tutorial

When WordPress 3.0 was released, all the hype was about something called “custom post types”.  Custom post types basically allow you to add your own content types to WordPress.  Lets say for instance that you want to create a newsletter.  A newsletter in your case, is a quick descriptions followed by the excerpts from several regular posts.  By default, WordPress doesn’t support this.  However, with custom post types we can add out own “Newsletter” post type and get to work.

Wordpress Newsletter Cusotm Post Type

Step 1:  Register the Custom Post Type

When creating a new custom post type, the first thing you need to do is register it with your WordPress install.  This can happen at the theme or plugin level.  In my trials with custom post types, I’ve always done it at the plugin level (mainly because I’m a developer, not a designer).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Set up custom post type variables.
$labels = array(
   'name' => _x('Newsletters', 'post type general name'),
   'singular_name' => _x('Newsletter', 'post type singular name'),
   'add_new' => _x('Add New', 'Newsletter'),
   'add_new_item' => __('Add New Newsletter'),
   'edit_item' => __('Edit Newsletter'),
   'new_item' => __('New Newsletter'),
   'view_item' => __('View Newsletter'),
   'search_items' => __('Search Newsletters'),
   'not_found' =>  __('Nothing found'),
   'not_found_in_trash' => __('Nothing found in Trash'),
   'parent_item_colon' => ''
);
 
$args = array(
   'labels' => $labels,
   'public' => true,
   publicly_queryable' => true,
   'show_ui' => true,
   'query_var' => true,
   'menu_icon' => get_stylesheet_directory_uri() . '/images/newsletter.gif',
   'rewrite' => true,
   'capability_type' => 'post',
   'hierarchical' => false,
   'menu_position' => 20,
   'supports' => array('title','editor', 'excerpt')
);
 
//Register the newsletter post type.
register_post_type( 'newsletter' , $args );

Now, lets disect this a bit so you know what’s going on.

  • Labels
    • name – The name of your custom post type.  Usually this is the plural form of it.
    • singular_name – This is the singular form of your plural name.
    • add_new – Normally when you add a post, you click “Add new”.  It’s the same thing for this example.  However, you could make it say “Be more awesome! Add a newsletter!” (if you REALLY wanted to).
    • edit_item – Same as add_new.  You can change how the edit link is displayed.
    • new_item – When you first create your new newletter, this is what it will describe it as.  Once you have a title on it, it will display that instead.
    • For more information on these, I suggest the WordPress Codex.
  • Args
    • labels – General label information for your custom post type.
    • public – Should this be made available to all users?
    • publicly_queryable – Should the public be able to run queries against your post type?
    • show_ui – Do you need a user interface?
    • menu_icon – Path to the icon that is displayed in the admin.
    • rewrite – Should WordPress attempt to make the urls friendly?
    • supports – This bit is pretty important.  This is where you describe what is shown in the admin.  Currently I have “title”, “editor”, and “excerpt”.  You can also extend this with your own stuff later.
    • For more information on these, I suggest the WordPress Codex.
  • register_post_type – This hook is how you register your shiny new custom post type with WordPress 3.0.  First argument is a unique name that you give to your custom post type.  The second argument is the array that was defined above.

Step 2: Custom Categories (Taxonomy) [optional]

One of the nice things about custom post types in WordPress 3 is that you don’t have to use the same categories (taxonomy) as your other posts  and pages.  Registering a new taxonomy for your custom post type is very easy.

1
2
3
4
5
6
7
8
9
//Create taxonomy for categorizing newsletters
register_taxonomy(
   "Categories",
   array("newsletter"),
   array("hierarchical" => true,
      "label" => "Categories",
      "singular_label" => "Category",
      "rewrite" => true)
);

How this works is fairly straight forward.  The first argument is what you’d like your new post type categories to be called.  In this example, I opted for simplicity and went with “Categories”.  The second argument is the post types that you would like this taxonomy to show up on.  Since we only want it on our new post type, I’ve defined it as such.  The third argument is for options (lables, rewrite on/off, etc).

Step 3:  Your done!

Really, it’s that easy.  If you want to make it really useful, you need to add meta boxes to admin interface so that you can do sweet custom content.  But at it’s bare minimum, this is all you need.

If there is sufficient interest, I can go into deeper detail about making a plugin with a custom post type.  Also, if you need help, drop a comment and I’d be happy to give your problem a shot.

Categories
PHP Programming

PHP Function Of The Day: ob_end_clean()

Sometimes you get in to a situation where you are working in an environment that drops everything into an output buffer before it spits it out the browser. In most languages this is called “output buffering”. The problem with having EVERYTHING buffered is being able to do special stuff like dynamic XML documents.

My solution to this little pickle was to just drop everything from the current output buffer and then kill the process after I’m done with it. To do this, just execute “ob_end_clean()” right before the functions/objects/code you need to execute. What “ob_end_clean()” does is drops all information that is currently stored in the output buffer, and then stops the buffering anything after it.

One “gotch ya” moment I had using this function was that it doesn’t end ALL cases of output buffering. If for some strange reason there is nested buffering going on, you’ll need to call the function as many times as it takes to get to the top of the call stack.

http://us2.php.net/manual/en/function.ob-end-clean.php

Categories
PHP Programming

Disabling Internet Explorer Cross Site Scripting Filter (XSS)

A client of mine recently tasked me with figuring out why the newer versions of IE were throwing a Cross Site Scripting (XSS) error.  For the life of me,  couldn’t figure out why.  Maybe it was because they were submitting a form to another server?  Or perhaps because the Javascript was closing the window when it was done?  I don’t know.  But, I did find a nice little trick that allows you to disable the Cross Site Scripting(XSS) filter in IE.

All that you need to do is add “X-XSS-Protection: 0” to the response header.  For instance, to disable the Cross Site Scripting(XSS) filter all you do is:

header(“X-XSS-Protection: 0”);

That’s it.  Usually that will resolve any XSS errors you have.  It may not be the best solution from a security stand point, but it’ll work in a pinch,

Categories
Other

Circular Queue

If you float around Computer Science / Programming circles enough, it’s likely that you’ve come across the term “circular queue”.  A circular queue is a queue of <something> that is fixed in size, and when the end of the queue has been reached, it circles back to the front and starts pushing items from there.  If the head catches up to the tail, the queue is said to be full.  In C style languages, circular queues can be implemented using pointers fairly easily.  In my case though, I don’t have pointers, so I’m just keeping two variables (head, tail) that tell me where the import parts are.

Some people might wonder “Why on earth are you implementing a queue by hand?”, which is valid question.  The answer is because there is now STL-type library in Limbo, so implementation of a queue falls into my lap.  I suppose this post doesn’t have much of point, except to say “Use the friggin’ library if it’s available.”.  For one, it’s already been done, and two, it’s probably bug free.

Categories
Other

Interesting Assignment

Recently in my graduate level operating systems course, we were assigned to make a networked semaphore manager.  Essentially, client machines can connect to the semaphore manager and ask it to control access to SOMETHING.  In our case, we’re just proving a point, so we’re controlling access standard error.  It’s an interesting problem because coding anything over a network is tough.  Secondly, creating a reliable semaphore manager is tough on it’s own.  To make matters worse, we’re using a hosted environment called Inferno.  Inferno was once created by Bell Labs, but is now distributed by Vitanuova.  For reference:

LimboOh, and it’s due in two days.  Taking a bit of time off was nice, but probably not the wisest decision.

Categories
Other

Weekend

This weekend I’m charged writing a networked semaphore manager.  In other news, I’m hanging out with Kelly all weekend.

Spring Break 2009
Spring Break 2009
Categories
Other Programming

Functional Programming with Erlang

For the past 8 weeks in my graduate operating systems course, we’ve been dealing with the issues of inter-process communication in massively parallel situations.  For development of such applications, we’ve been using an operating system called Inferno (of Plan 9 origins) which has a built in language called Limbo.  Limbo is a great language for learning network programming and multi-threaded programming.  It has communication channels which are typed, so you can pass whatever you want along them without pre-processing the data.  However, you still have to manage all of those channels.  It also has a nice C + Pascal style syntax:

message := “Hello World!”;
sys->print(“%s\n”, message);

While discussing languages that support massive concurrency with my professor, the subject of functional programming came up.  I mentioned to him that I wanted to learn a functional language, but wasn’t sure where to start.  He suggested Erlang due to it’s easy support for massive concurrency.  My question to my readers (if I have any left), is do you have any experience with programming multi-threaded and/or distributed programs with Erlang?  Is it worth the time to learn, or would my efforts be better off elsewhere?

Categories
Other

First Week as a Teaching Assistant

This week (Monday), I started my position as a teaching assistant for the Computer Science department at Central Michigan University.  As this was my first time as a TA, they thought it would be prudent to give me lab sections of CPS 100 (“Computers and Society”).

I have 5 sections of CPS 100, with roughly 45 people in each section.  I thought that my lab sections have gone well so far.  In fact, I even learned some things.

  • No matter how many times you explain it, some students will still staple the papers in the wrong order.
  • Stapling papers in the correct order doesn’t matter.
  • Students are generally very surprised when I walk in and stand up by the podium.  Apparently I look young.
  • The students are generally very cordial and nice when dealing with me.  Most don’t want to be there, so it’s all I could ask for I guess.
  • The technology required for Lab WILL fail.  This is not the end of the world, just kind of roll with it and there won’t be any problems.
  • I am old.  Someone called me “Mr. Slingerland”.
  • It feels nice when someone approaches you, apologizing profusely for not bringing a flash drive, because the course syllabus said that they would need one.  If everyone was this dedicated, my job would be way too easy.
  • There is a strong camaraderie between the grad assistants in the department.  We all can relate to each other, so what seems like instant friendships have formed.

That’s all I’ve really learned by teaching so far.  The first lab excercise was really simple, and having done it before class helped a lot with being prepared.

Besides that, I have 3 courses this semester.

  • CPS 650 – Compiler Construction
  • CPS 670 – Operating Systems
  • CPS 685 – Pattern Recognition and Data Mining

All these courses interest me, so I feel that I’ll do well this semester.  Between that and having an office (that I share with 4 other people) on campus, it’ll make getting homework done very easy.