├── .gitignore ├── README.md └── custom-post-type.php /.gitignore: -------------------------------------------------------------------------------- 1 | custom-post-types.sublime-project 2 | custom-post-types.sublime-workspace 3 | .DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bamboo 2 | ### WordPress Custom Post Types 3 | 4 | > Bamboo - WordPress Custom Post Types is a PHP class to help register and maintain WordPress custom post types. It also comes with some rad built-in properties and methods that can be used in templates to maintain clean code and modular development. 5 | 6 | For more information about registering post types, including a full list of options, visit the [WordPress Codex](http://codex.wordpress.org/Function_Reference/register_post_type). 7 | 8 | This class works well with the [WordPress Custom Taxonomy class](https://github.com/beaucharman/wordpress-custom-taxonomy). They were made for each other <3. 9 | 10 | ### Declaring New Post Types 11 | 12 | Include [`custom-post-type.php`](https://github.com/beaucharman/wordpress-custom-post-types/blob/master/custom-post-type.php) in your `functions.php` file, with something like `require_once(get_template_directory() . '/includes/custom-post-types.php');`. 13 | 14 | Declare the various argument arrays (within `funtions.php` or another include file) to setup the new post type as needed (`name` is required): 15 | 16 | ```PHP 17 | // required 18 | $args['name'] = ''; 19 | 20 | // optional 21 | $args['labels'] = array( 22 | 'singular' => '', 23 | 'plural' => '', 24 | 'menu' => '' 25 | ); 26 | 27 | $args['options'] = array( 28 | 'public' => true, 29 | 'hierarchical' => false, 30 | 'supports' => array('title', 'editor', 'thumbnail'), 31 | 'has_archive' => true 32 | ); 33 | 34 | $args['menu_icon'] = ''; // The Unicode of the desired font 35 | 36 | $args['help'] = array( 37 | array( 38 | 'message' => '' 39 | ), 40 | array( 41 | 'context' => 'edit', 42 | 'message' => '' 43 | ) 44 | ); 45 | ``` 46 | Then create a variable (for future reference, but is not required) from an instance of the `bamboo_Custom_Post_Type` class: 47 | 48 | ```PHP 49 | $PostType = new Bamboo_Custom_Post_Type($args); 50 | ``` 51 | 52 | You can even quick declare a Post Type by just passing the name as a string. So for a Post Type of *movie*, just pass: 53 | 54 | ```PHP 55 | $Movie = new Bamboo_Custom_Post_Type('movie'); 56 | ``` 57 | 58 | ### Usage 59 | 60 | The custom post type class creates a handfull of useful properties and methods that can be accessed through the post type's instance variable and can be used in template and admin files. 61 | 62 | #### Properties 63 | 64 | **`$PostType->name`** 65 | 66 | The post type slug. 67 | 68 | **`$PostType->lables`** 69 | 70 | An array of the singular, plural and menu lables. 71 | 72 | #### Methods 73 | 74 | **`$PostType->get()`** 75 | 76 | Get all entries assigned to this post type. Accepts an array of arguments, and a boolean value to retrieve just a single value (true, useful to use along side `'include' => $single_id`) or an array of results (false). 77 | 78 | For example: 79 | 80 | ```PHP 81 | $post_types = $PostType->get(); 82 | ``` 83 | 84 | **Note:** A declaration of `global $PostType;` might be required on some template files to make use of the required post type's instance. 85 | 86 | See the [Get Posts => Default Usage](http://codex.wordpress.org/Template_Tags/get_posts#Default_Usage) codex reference for the list of possible arguments, and the [Get Pages => Return](http://codex.wordpress.org/Function_Reference/get_pages#Return) codex reference for the list of return values. 87 | 88 | #### Custom Icon 89 | 90 | **`get_font_awesome()`** 91 | 92 | To utilise the *menu_icon* feature of this class, *[Font Awesome](http://fortawesome.github.io/Font-Awesome/)* needs to be reference, and there is nice helper function included in Bamboo that will include *Font Awesome* in the admin for you. Just call the method before declareing any post types: 93 | 94 | ```PHP 95 | /* Include Font Awesome */ 96 | Bamboo_Custom_Post_Type::get_font_awesome(); 97 | ``` 98 | 99 | #### Working with Custom Taxonomies 100 | 101 | To get posts within (or maybe even not within) terms of particular taxonomies, you can use the [Tax Query](https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters) option within your `get` function's `$args`. For example: 102 | 103 | ```PHP 104 | $args = array( 105 | 'tax_query' => array( 106 | array( 107 | 'taxonomy' => 'foo_category_slug', 108 | 'field' => 'bar_term_slug', 109 | 'terms' => array('qux', 'baz'), 110 | 'include_children' => true, 111 | 'operator' => 'IN' 112 | ) 113 | ) 114 | ); 115 | 116 | $post_types = $PostType->get($args); 117 | ``` 118 | 119 | ### Flush Rewrites 120 | 121 | If there are issues with permalinks and the new post types, even after flushing them in the administrator area (Settings > Permalinks > Save Changes), use the following function to flush permalink rewrites for new custom post types and taxonomies. 122 | 123 | ```PHP 124 | add_action('init', 'bamboo_flush_rewrites'); 125 | function bamboo_flush_rewrites() 126 | { 127 | global $wp_rewrite; 128 | $wp_rewrite->flush_rules(); 129 | } 130 | ``` 131 | -------------------------------------------------------------------------------- /custom-post-type.php: -------------------------------------------------------------------------------- 1 | name {string} 13 | * $PostType->lables {array} 14 | * 15 | * Methods 16 | * $PostType->get() 17 | * 18 | * To declare a custom post type, simply create a new instance of the 19 | * Bamboo_Custom_Post_Type class. 20 | * 21 | * Configuration guide: 22 | * https://github.com/beaucharman/wordpress-custom-post-types 23 | * 24 | * For more information on registering post types: 25 | * http://codex.wordpress.org/Function_Reference/register_post_type 26 | */ 27 | 28 | 29 | 30 | /* ======================================================================== 31 | Custom Post Type class 32 | ======================================================================== */ 33 | 34 | 35 | 36 | class Bamboo_Custom_Post_Type 37 | { 38 | 39 | public $name; 40 | public $labels; 41 | public $options; 42 | public $icon; 43 | public $help; 44 | 45 | 46 | 47 | /** 48 | * Class Constructor 49 | * ======================================================================== 50 | * @param {array} $args 51 | * @return {instance} post type 52 | */ 53 | public function __construct($args) 54 | { 55 | /** 56 | * Set class values 57 | */ 58 | if (! is_array($args)) 59 | { 60 | $name = $args; 61 | $args = array(); 62 | } 63 | else 64 | { 65 | $name = $args['name']; 66 | } 67 | 68 | $args = array_merge( 69 | array( 70 | 'name' => $this->uglify_words($name), 71 | 'labels' => array(), 72 | 'options' => array(), 73 | 'menu_icon' => null, 74 | 'help' => null 75 | ), 76 | $args 77 | ); 78 | 79 | $this->name = $args['name']; 80 | $this->labels = $args['labels']; 81 | $this->options = $args['options']; 82 | $this->icon = $args['menu_icon']; 83 | $this->help = $args['help']; 84 | 85 | /** 86 | * Create the labels where needed 87 | */ 88 | 89 | /* Post type singluar label */ 90 | if (! isset($this->labels['singular'])) 91 | { 92 | $this->labels['singular'] = $this->prettify_words($this->name); 93 | } 94 | 95 | /* Post type plural label */ 96 | if (! isset($this->labels['plural'])) 97 | { 98 | $this->labels['plural'] = $this->plurify_words($this->labels['singular']); 99 | } 100 | 101 | /* Post type menu label */ 102 | if (! isset($this->labels['menu'])) 103 | { 104 | $this->labels['menu'] = $this->labels['plural']; 105 | } 106 | 107 | /** 108 | * If the post type doesn't already exist, create it! 109 | */ 110 | if (! post_type_exists($this->name)) 111 | { 112 | add_action('init', array(&$this, 'register_custom_post_type')); 113 | 114 | if ($this->help) 115 | { 116 | add_action('contextual_help', array(&$this, 'add_custom_contextual_help'), 10, 3); 117 | } 118 | 119 | if ($this->icon) 120 | { 121 | add_action('admin_head', array(&$this, 'icon_style')); 122 | } 123 | } 124 | } 125 | 126 | 127 | 128 | /** 129 | * Register Custom Post Type 130 | * ======================================================================== 131 | * @param {null} 132 | * @return post type 133 | */ 134 | public function register_custom_post_type() 135 | { 136 | /** 137 | * Set up the post type labels 138 | */ 139 | $labels = array( 140 | 'name' => __($this->labels['plural']), 141 | 'singular_name' => __($this->labels['singular']), 142 | 'menu_name' => __($this->labels['menu']), 143 | 'add_new_item' => __('Add New ' . $this->labels['singular']), 144 | 'edit_item' => __('Edit ' . $this->labels['singular']), 145 | 'new_item' => __('New ' . $this->labels['singular']), 146 | 'all_items' => __('All ' . $this->labels['plural']), 147 | 'view_item' => __('View ' . $this->labels['singular']), 148 | 'search_items' => __('Search ' . $this->labels['plural']), 149 | 'not_found' => __('No ' . $this->labels['plural'] . ' found'), 150 | 'not_found_in_trash' => __('No ' . $this->labels['plural'] . ' found in Trash') 151 | ); 152 | 153 | /** 154 | * Configure the post type options 155 | */ 156 | $options = array_merge( 157 | array( 158 | 'has_archive' => true, 159 | 'labels' => $labels, 160 | 'menu_icon' => null, 161 | 'menu_position' => 4, 162 | 'public' => true, 163 | 'rewrite' => array('slug' => $this->get_slug()), 164 | 'supports' => array('title', 'editor', 'thumbnail', 'revisions') 165 | ), 166 | $this->options 167 | ); 168 | 169 | /** 170 | * Register the new post type 171 | */ 172 | register_post_type($this->name, $options); 173 | } 174 | 175 | 176 | 177 | /** 178 | * Add Custom Contextual Help 179 | * ======================================================================== 180 | * @param $contextual_help 181 | * @param $screen_id 182 | * @param $screen 183 | * @return $contextual_help 184 | */ 185 | public function add_custom_contextual_help($contextual_help, $screen_id, $screen) 186 | { 187 | foreach ($this->help as $help) 188 | { 189 | if (! isset($help['context'])) 190 | { 191 | $context = $this->name; 192 | } 193 | else 194 | { 195 | $context = $help['context'] . '-' . $this->name; 196 | } 197 | 198 | if ($context == $screen->id) 199 | { 200 | $contextual_help = $help['message']; 201 | } 202 | } 203 | 204 | return $contextual_help; 205 | } 206 | 207 | 208 | 209 | /** 210 | * Get 211 | * ======================================================================== 212 | * @param {array} $user_args 213 | * @param {boolean} $single 214 | * @return {array} post type data 215 | * 216 | * Get all entries assigned to this post type. 217 | */ 218 | public function get($user_args = array(), $single = false) 219 | { 220 | $args = array_merge( 221 | array( 222 | 'posts_per_page' => -1, 223 | 'orderby' => 'title', 224 | 'order' => 'ASC', 225 | 'post_type' => $this->name, 226 | 'post_status' => 'publish' 227 | ), 228 | $user_args 229 | ); 230 | 231 | $items = get_posts($args); 232 | 233 | if ($single) 234 | { 235 | return $items[0]; 236 | } 237 | 238 | return $items; 239 | } 240 | 241 | 242 | 243 | /** 244 | * Get Slug 245 | * ======================================================================== 246 | * @param {string} $name 247 | * @return {string} 248 | */ 249 | public function get_slug($name = null) 250 | { 251 | if (! $name) 252 | { 253 | $name = $this->name; 254 | } 255 | 256 | return strtolower(str_replace(' ', '-', str_replace('_', '-', $name))); 257 | } 258 | 259 | 260 | 261 | /** 262 | * Prettify Words 263 | * ======================================================================== 264 | * @param {string} $words 265 | * @return {string} 266 | * 267 | * Creates a pretty version of a string, like a pug version of a dog. 268 | */ 269 | public function prettify_words($words) 270 | { 271 | return ucwords(str_replace('_', ' ', $words)); 272 | } 273 | 274 | 275 | 276 | /** 277 | * Uglify Words 278 | * ======================================================================== 279 | * @param {string} $words 280 | * @return {string} 281 | * 282 | * Creates a url firendly version of the given string. 283 | */ 284 | public function uglify_words($words) 285 | { 286 | return strToLower(str_replace(' ', '_', $words)); 287 | } 288 | 289 | 290 | 291 | /** 292 | * Plurify Words 293 | * ======================================================================== 294 | * @param {string} $words 295 | * @return {string} 296 | * 297 | * Plurifies most common words. Not currently working proper nouns, 298 | * or more complex words, for example knife => knives, leaf => leaves. 299 | */ 300 | public function plurify_words($words) 301 | { 302 | if (strToLower(substr($words, -1)) == 'y') 303 | { 304 | return substr_replace($words, 'ies', -1); 305 | } 306 | 307 | if (strToLower(substr($words, -1)) == 's') 308 | { 309 | return $words . 'es'; 310 | } 311 | 312 | return $words . 's'; 313 | } 314 | 315 | 316 | 317 | /** 318 | * Icon Style 319 | * ======================================================================== 320 | * @param {null} 321 | * @return {output} html 322 | */ 323 | public function icon_style() { ?> 324 | 329 | '; 347 | } 348 | } 349 | } 350 | --------------------------------------------------------------------------------