Wallogit.com
2017 © Pedro Peláez
Post type helper for WordPress
The PostType component uses two classes: - AbstractPostType, which you extend to create a PostType. - PostTypeHelper, which hooks into everything for you and registers the post types., (*1)
Lets create a pimple config for both of these, (*2)
$pimple["documentPostType"] = function () {
return new Document();
};
$pimple['PostTypeHelper'] = function ($c) {
return new PostTypeHelper($c['objectStorage'], array(
$c["documentPostType"]
));
};
Create a concreate class that implements the AbstractPostType class and implements the getName() method., (*3)
class DocumentType extends AbstractPostType {
public function getName()
{
return "document";
}
}
This above example is the bare minimum you must implement, the example that follows is the other extreme implementing all available methods., (*4)
class Document extends AbstractPostType {
public function getSingularName()
{
return "Doc";
}
public function getPluralName()
{
return "Docs";
}
public function getArgs()
{
$args = parent::getArgs();
$args['supports'] = array("title", "editor");
return $args;
}
protected function getLabels()
{
$labels = parent::getLabels();
$labels['menu_name'] = 'Docs';
return $labels;
}
public function getName()
{
return "document";
}
}
Once you have setup the pimple config you are use the PostTypeHelper like this, (*5)
$helper = $pimple['postTypeHelper']; $helper->registerPostTypes();
That's it, the helper will then add all the needed hooks and register all the post types you have provided it., (*6)