Snippets
Created by
James Kemp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | <?php
if( class_exists( 'JCK_Helper_Post_Types' ) )
return;
/**
* Helper Class for dealing with Post Types
*
* @version 1.0.1
* @author James Kemp
*/
class JCK_Helper_Post_Types {
/**
* Array of all post types registered by this plugin
*
* @since 1.0.0
* @var array $registered
*/
public $registered = array();
/**
* Method: Add
*
* @since 1.0.0
* @param array $options
*/
public function add( $options ) {
$defaults = array(
"plural" => "", // !required
"singular" => "", // !required
"key" => false, // !required
"rewrite_slug" => false, // !recommended if has frontend visibility
"rewrite_with_front" => false,
"rewrite_feeds" => true,
"rewrite_pages" => true,
"menu_icon" => "dashicons-admin-post",
'hierarchical' => false,
'supports' => array( 'title' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'capability_type' => 'post'
);
$options = wp_parse_args( $options, $defaults );
if( $options['key'] ) {
$labels = array(
'name' => $options['plural'],
'singular_name' => $options['singular'],
'add_new' => _x( 'Add New', 'jck' ),
'add_new_item' => _x( sprintf('Add New %s', $options['singular']), 'jck' ),
'edit_item' => _x( sprintf('Edit %s', $options['singular']), 'jck' ),
'new_item' => _x( sprintf('New %s', $options['singular']), 'jck' ),
'view_item' => _x( sprintf('View %s', $options['singular']), 'jck' ),
'search_items' => _x( sprintf('Search %s', $options['plural']), 'jck' ),
'not_found' => _x( sprintf('No %s found', strtolower($options['plural'])), 'jck' ),
'not_found_in_trash' => _x( sprintf('No %s found in Trash', strtolower($options['plural'])), 'jck' ),
'parent_item_colon' => _x( sprintf('Parent %s:', $options['singular']), 'jck' ),
'menu_name' => $options['plural']
);
$args = array(
'labels' => $labels,
'hierarchical' => $options['hierarchical'],
'supports' => $options['supports'],
'public' => $options['public'],
'show_ui' => $options['show_ui'],
'show_in_menu' => $options['show_in_menu'],
'menu_icon' => $options['menu_icon'],
'show_in_nav_menus' => $options['show_in_nav_menus'],
'publicly_queryable' => $options['publicly_queryable'],
'exclude_from_search' => $options['exclude_from_search'],
'has_archive' => $options['has_archive'],
'query_var' => $options['query_var'],
'can_export' => $options['can_export'],
'capability_type' => $options['capability_type'],
'rewrite' => false
);
if( $options['rewrite_slug'] ) {
$args['rewrite'] = array(
"slug" => $options['rewrite_slug'],
"with_front" => $options['rewrite_with_front'],
"feeds" => $options['rewrite_feeds'],
"pages" => $options['rewrite_pages']
);
}
$post_type = register_post_type( $options['key'], $args );
$this->registered[] = $post_type;
}
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.