vernon/powerposts

WordPress custom post types, taxonomies and metabox library


Keywords
wordpress, post, taxonomies, metaboxes
License
MIT

Documentation

Custom post types, taxonomies and meta boxes with Power Posts

A facade that can be used to easely create custom post types, taxonomies and metaboxes. Please note that this README is still in the process of being written and so is Power Posts

The Installation

To use Power Posts all you need to do is download the files and place them somewhere in your themes folder. After that simply just require the main class file in your themes functions.php file as shown below.

require_once('inside_theme_folder/power-posts/power-post.php');

Using PowerPosts

In the following examples I will explain how you can use the Power Post class library to create custom post types, taxonomies and meta boxes for your WordPress theme.

Adding Custom Post Types

To create the custom post type simply create a new Power_Posts object inside of your themes functions.php file and pass in the required arguments into the constructor.

PowerPost constructor arguments
  1. Post type name in singular form (string) - required
  2. Post type name in plural form (string) - required
  3. WordPress register post type arguments - (array) - optional
    • This array will be used to override the default arguments created by the PowerPost class. For more information about the array options you can look at the arguments section in the following WordPress documentation page.
Simple example
// required arguments only
$games = new Power_Post('Game', 'Games');
Advanced example
// with optional array argument
$games = new Power_Post('Game', 'Games', [
    'menu_icon' => 'dashicons-smiley',
    'supports'  => [ 'thumbnail', 'editor', 'excerpt', 'title' ]
]);

Adding Taxonomies

To add a taxonomy to our Games post type that we created above, simply just create a new instance of the Power_Post_Taxonomy class.

PowerPostTaxonomy constructor arguments
  1. Taxonomy name in singular form (string) - required
  2. Taxonomy name in plural form (string) - required
  3. WordPress register taxonomy arguments - (array) - optional
    • This array will be used to override the default arguments created by the PowerPostTaxonomy class. For more information about the array options you can look at the arguments section in the following WordPress Documentation page.
Simple example
// required arguments only
$games = new Power_Post('Game', 'Games');
$games_taxonomy = new Power_Post_Taxonomy($games->get_post_type_id(), 'Game Category', 'Game Categories');
Advanced example
// with optional array argument
$games = new Power_Post('Game', 'Games', [
    'menu_icon' => 'dashicons-smiley',
    'supports'  => [ 'thumbnail', 'editor', 'excerpt', 'title' ]
]);

// with optional array argument
$games_taxonomy = new Power_Post_Taxonomy( $games->get_post_type_id(), 'Game Category', 'Game Categories', [
    'hierarchical'      => true,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array( 'slug' => 'genre' )
]);

Adding Meta Boxes

To add a meta box to our edit screen create a new Power_Post_Meta_Box instance.

$games = new Power_Post('Game', 'Games');
$games_taxonomy = new Power_Post_Taxonomy($games->get_post_type_id(), 'Game Category', 'Game Categories');
$games_metabox = new Power_Post_Meta_Box($games->get_post_type_id(), 'Developer Information', array());

Adding Input Fields

At this point in time there's only a couple input types available but more will be added soon.

  1. text field (Input_Text_Field)
  2. textarea field (Input_Text_Area)
  3. date picker field (Input_Date_Picker)
  4. selection box (Input_Option_Selection)
  5. time (Input_Time)
$games = new Power_Post('Game', 'Games');
$games_taxonomy = new Power_Post_Taxonomy($games->get_post_type_id(), 'Game Category', 'Game Categories');
$games_metabox = new Power_Post_Meta_Box($games->get_post_type_id(), 'Developer Information', array(
   new Input_Text('First Name'),
   new Input_Text('Last Name'),
   new Input_Text_Area('Description'),
   new Input_Date_Picker('Date Picker'),
   new Input_Date('Date'), // html5 only
   new Input_Time('Time'), // html5 only
));