Created
May 1, 2012 02:10
-
-
Save FernE97/2564430 to your computer and use it in GitHub Desktop.
PHP: WordPress Custom Post Types Dynamic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // requires php5.3 | |
function add_post_type( $name, $args = array() ) { | |
add_action( 'init', function() use( $name, $args ) { | |
$upper = ucwords( str_replace( '_', ' ', $name ) ); | |
$lower = strtolower( str_replace( '_', ' ', $name ) ); | |
$name = strtolower( str_replace( ' ', '_', $name ) ); | |
$labels = array( | |
'name' => _x( $upper . 's', $name ), | |
'singular_name' => _x( $upper, $name ), | |
'add_new_item' => _x( "Add New $upper", $name ), | |
'edit_item' => _x( "Edit $upper", $name ), | |
'new_item' => _x( "New $upper", $name ), | |
'view_item' => _x( "View $upper", $name ), | |
'search_items' => _x( "Search $upper" . 's', $name ), | |
'not_found' => _x( "No $lower" . 's found', $name ), | |
'not_found_in_trash' => _x( "No $lower" . 's found in Trash', $name ), | |
'parent_item_colon' => _x( "Parent $upper:", $name ), | |
'menu_name' => _x( "$upper" . 's', $name ) | |
); | |
$args = array_merge( | |
array( | |
'public' => true, | |
'labels' => $labels, | |
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ), | |
'has_archive' => true, | |
'rewrite' => array( 'slug' => "$name" . 's' ) | |
), | |
$args | |
); | |
register_post_type( $name, $args ); | |
}); | |
} | |
function add_taxonomy( $name, $post_type, $args = array() ) { | |
add_action( 'init', function() use( $name, $post_type, $args ) { | |
$name = strtolower( str_replace( ' ', '_', $name ) ); | |
$args = array_merge( | |
array( | |
'label' => ucwords( str_replace( '_', ' ', $name ) ) | |
), | |
$args | |
); | |
register_taxonomy( $name, $post_type, $args ); | |
}); | |
} | |
// now add post types and taxonomies from functions.php | |
add_post_type ( 'movie', array( | |
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' ) | |
)); | |
add_taxonomy( 'genre', 'movie' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment