Skip to content

Instantly share code, notes, and snippets.

@GreenGeorge
Created April 22, 2012 11:03
Show Gist options
  • Save GreenGeorge/2463571 to your computer and use it in GitHub Desktop.
Save GreenGeorge/2463571 to your computer and use it in GitHub Desktop.
Functions to quickly automate making custom post types in wordpress. Heavily based on Jeffrey Way's tutorial on nettuts
<?php
// ***** Declare function that buids the post type
function add_post_type($name, $args = array() ) {
add_action('init',function() use($name, $args) {
// make post type name capitalized
$upper = ucwords($name);
// make name acceptable
$name = strtolower(str_replace(' ', '_', $name));
// merge default args with passed args
$args = array_merge(
array(
'public' => true,
'label' => ucwords("$name" . 's'),
'labels' => array(
'add_new_item' => "Add New $upper" . ''),
'supports' => array(
'title','editor','excerpt'),
'taxonomies' => array('category')
),$args);
// build post type
register_post_type($name, $args );
});
};
// ***** Declare function that builds the taxonomy
function add_taxonomy($name, $post_type, $args = array()) {
add_action('init', function() use($name, $post_type, $args) {
// make taxonomy name capitalized
$upper = ucwords($name);
$plural = ucwords("$name" . 's');
// make name acceptable
$name = strtolower(str_replace(' ', '_', $name));
// merge default args with passed args
$args = array_merge(array(
'label' => $plural,
),$args);
register_taxonomy($name, $post_type, $args);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment