Custom Post Type Helper Class

Tento článek (nebo jeho část) je převzat z externího zdroje. Je tedy slušností jej uvést včetně případného autora.

For a lot of WordPress projects these days we use custom post types. The WordPress development team created some handy methods to integrate them into your projects. But when you use custom post types, taxonomies and meta boxes frequently, it’s quite probable that you’re going to repeat yourself. That’s why we are going to use the power of these WordPress functions to build a more powerful class, which we can use to quickly register post types, taxonomies and meta boxes.

This is how we call our class when it’s done.

We start off with creating the class, main properties, constructor and methods. In this tutorial we will fill them with our programming logic.

Within the constructor we create some important variables, which are used within the entire class. We also call add_action to register the post type and we listen for when the post type is being saved, so we can save our post’s meta data. If the post type exists, the add_action is not called, but the $post_type_name is set, so we can add taxonomies and meta boxes to it.

Within the register_post_type method, which gets called by the add_action in the constructor, we first determine the name (capitalized) and the plural. With this name and plural we build our labels for the post type and overwrite (and merge) them with the given labels from the $this->post_type_labels variable. Then we create our arguments with the same principle.

First we check if the $name parameter is empty. When it is, we do nothing. When it’s not, we create three variables in which we store the information for the taxonomy: $taxonomy_name$taxonomy_labels and $taxonomy_args.

After we’ve done the first checks and then set some variables, we’re going to register the post type. But first we check if the taxonomy already exists.

If the taxonomy doesn’t exist, we register it. We use an add_action, but not in the normal way. Normally, the second parameter of the add_action is the name of a function, but since we use different parameters each time, we are going to pass a nameless function (Note: this feature requires PHP 5.3+) and use the use() function. With the use() function we can pass variables to the nameless function. This time we need to pass $taxonomy_name$post_type_name and $taxonomy_args to register the taxonomy.

When the taxonomy doesn’t exist, we only attach it to our post type. Just like before, we use a nameless function and the use() function. This time we only need to pass $taxonomy_name and $post_type_name.

For registering meta boxes, we need the post type name, so first we determine that. After that we need some variables for the meta box itself and we make the custom meta fields global, so we can access them in the save hook. We won’t cover too much detail here, because Tammy Hart made a very useful tutorial about reusable meta boxes already.

When we set the variables and globals, we register the meta box with an add_action. Like before, we use a nameless function. This time we need $box_id$box_title$post_type_name$box_context$box_priority and $fields.

Save all the post’s meta data. We loop through them, using the global $custom_fields. This is also a quick coverage, see Tammy Hart’s tutorial about reusable meta boxes.

As you can see we use strtolower( str_replace( ' ', '_', $string ) ) and ucwords( str_replace( '_', ' ', $string ) ) a number of times. The reason of creating this class is that we don’t repeat ourselves, so we don’t want to do that in this part either. That’s why we create some helper methods. In this way we can do this: $name = self::beautify( $string ); instead of $name = strtolower( str_replace( ' ', '_', $title ) );

Another point is the plural forms we create. We just create them by adding an ‚s‘ to the word. But what happens when the word ends with a ‚y‘? For this reason we create a helper method to determine the plural form of a word. Now we can easily do this: $plural = self::pluralize( $string ) and the plural form of our word will be determined.

And now we’re done. You can now use this class to easily register post types, taxonomies and meta boxes. If you have any suggestions or questions, just leave a comment, so we can talk about it. Hope to see you next time!

Also, I would like to give some credit to Jeffrey Way. I used his class as inspiration for my class and for this tutorial. Also, I would like to give some credit to Tammy Hart, for building the reusable meta boxes tutorial. Take a look at their work.