Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.