• Aucun résultat trouvé

WordPress uses built-in post types for normal everyday use, but you can extend the list of available post types by using a pivotal function: register_post_type(). It is this function that allows WordPress to fulfill more traditional CMS roles. For example, you could create a custom post type for "movies" with custom fields for "plot", "genre" and

"rating". The important thing to realize is that any custom post types will be visible to WordPress only if you register them using the register_post_type() function, and as you may have guessed, the only way for you to do that is inside a plugin.

To really understand what this complex function does, we really need to get our hands dirty and try using it ourselves. You guessed it, it's time for you to create another plugin.

We are naming this plugin "Content Chunks", so our first step is to create a folder named content-chunks in the wp-content/plugins folder, and then create our index.php with a valid information header.

<?php /*

Plugin Name: Content Chunks Plugin URI: http://tipsfor.us/

Description: Display Chunks of Content from a custom post type "chunk"

Author: Everett Griffiths

You can see we have already added the action we intend to use, so the next step should be no surprise; create an includes folder, and in it create a file named ContentChunks.php:

public static function register_chunk_post_type() {

'parent_item_colon' => '9. Parent Chunk Colon', /*

??? */

'menu_name' => '10. Chunks', /* ??? */

),

'description' => 'Reusable chunks of content', 'public' => true,

'publicly_queryable' => true,

// 'exclude_from_search' => false, /* optional */

'show_ui' => true,

'show_in_menu' => true, 'menu_position' => 5,

//'menu_icon' => '', /* optional string */

'capability_type' => 'post',

//'capabilities' => array(), /* optional */

//'map_meta_cap' => false, /* optional */

'hierarchical' => true,

'supports' => array('title','editor','author','thumbnail' ,'excerpt','trackbacks','custom-fields','comments','revisions','page-attributes'),

'register_meta_box_cb' => '', /* optional callback */

// 'taxonomies' => array('category'), /* optional */

// 'permalink_epmask' => EP_PERMALINK, /* optional */

// 'has_archive' => false,

'rewrite' => false, /* optional - can be an array */

'query_var' => false, /* boolean or string */

'can_export' => false, 'show_in_nav_menus' => true, )

);

} } /*EOF*/

As always, we begin by mocking things up so that we have a functional outline to test. Save your files and then refresh your browser while logged into the Dashboard and correct any typos in your code. Here we will give you all possible parameters to the register_post_type() function, even the optional ones.

We have to pause to remind you not to write functions like this. It has a ridiculous number of inputs that are very difficult to keep straight. To make matters worse, the official documentation at present is rather poor (http://goo.gl/LtE3). Our final gripe is that the data types of many input options are inconsistent. It should be a red flag if your functions have inputs like "boolean or string"—that usually is an indication of a poorly architected function. At a minimum, such functions are more demanding on you (the developer) because you have to sanitize the input parameters.

We get the feeling that this function is half-baked, and we are hoping that future versions of WordPress will include a better implementation, or at least more-thorough documentation. In our opinion, this function should have been broken down into several more manageable object methods, but WordPress tends to favor the all-in-one approach, and in this case, it seems to have been the wrong approach.

Congratulations for making it through all of that—you should now have your very own custom post type. If you were successful, once you activate your plugin, you should see Chunks listed in the left-hand administration menu alongside the other post types.

One of the most confusing areas of the register_post_type() function is the labels. These labels are part of an optional parameter, but it makes for a nicer user experience if you take the time to provide them. We have numbered our parameters in our code to help demonstrate where each label will appear.

We could not locate the parent_item_colon or the menu_name labels.

We encourage you to play around with the $supports array and watch how each setting affects the manager. Try creating a new Chunk and look at all the different parts of the editing page. Almost every component of the editor can be changed by the $supports array.

We have outlined the components here as a reference:

Note that the thumbnail image option requires that you enable thumbnail support for each content type by including the add_theme_support() function. Typically this function appears in the active theme's functions.php file, but you can put it immediately after the register_post_type() function in the ContentChunks.php file, just so you see how it works:

add_theme_support( 'post-thumbnails', array('chunk') );

Without that line, the "Featured Image" meta box would not be visible.

The thing you might not realize when looking at these options is that several of them are interconnected in unforeseen ways, and unless you are extremely patient, it is difficult to figure out how to use all of them. It's no accident that there are practically no tutorials out there for how to accomplish certain tasks related to custom post types. The reality is that the documentation is lacking or absent, and figuring out how to accomplish some of the tasks is beyond the average developer. Again, if you want to see this in a fuller implementation, take a look at our Custom Content Type Manager plugin (http://goo.gl/cgJDU); it doesn't qualify as "easy to understand", but it can help you get a sense of what custom post types can do.

Now that we know how to control the appearance of the manager by altering the inputs to the register_post_type() function, let's configure them so our new

"chunk" post type will have the attributes we want.