Cornerstone: Creating Custom Content Types in WordPress
Cornerstone’s Content Type functionality provides you with even more control over how you organize and add your content in WordPress. Learn more about what makes Content Types awesome.
Here’s a quick tutorial on how to use Content Types in Cornerstone.
Content Types are created programmatically like so:
$bk =& new CNR_Content_Type('book'); $bk->set_title('Book');
From here you can add some custom fields:
$bk->add_field('author', 'text', array('size' => 25, 'label' => 'Author')); $bk->add_field('publisher', 'text', array('size' => 15, 'label' => 'Publisher'));
The fields need to be added to groups for display on the admin edit form:
//Create group $bk->add_group('details', 'Details'); //Add Fields to group $bk->add_to_group('details', array('author','publisher'));
Once you’re done adding fields, the new custom content type can be registered:
$bk->register();
Finally, the code needs to be called via the cnr_register_content_types
action so that the content type is registered at the proper time. So we wrap out content type creation code into a function and hook it into the action:
function my_content_type() { $bk =& new CNR_Content_Type('book'); $bk->set_title('Book'); //Add fields $bk->add_field('author', 'text', array('size' => 25, 'label' => 'Author')); $bk->add_field('publisher', 'text', array('size' => 15, 'label' => 'Publisher')); //Create group $bk->add_group('details', 'Details'); //Add Fields to group $bk->add_to_group('details', array('author','publisher')); //Register $bk->register(); } add_action('cnr_register_content_types', 'my_content_type');
Now, when you log in to the admin, you’ll see the new custom content type in the menu
And when you add/edit a new item, you’ll have fields necessary for your custom content type
Next Step
Learn how to use the data from these fields in the next tutorial.