Skip to content

Instantly share code, notes, and snippets.

@derekshirk
Last active July 26, 2016 16:19
Show Gist options
  • Save derekshirk/f76e5aad15ce6e0797078440e2a2ea6d to your computer and use it in GitHub Desktop.
Save derekshirk/f76e5aad15ce6e0797078440e2a2ea6d to your computer and use it in GitHub Desktop.
A Better Approach to Registering Custom Post Types for WordPress
<?php /*
/* ------------------------------------------------- */
/* Use Variables to Make Registering CPTs Easier
/* ------------------------------------------------- */
function register_my_cpt_name() {
$name = 'My Custom Post Type' ;
$singular = 'The Custom Post Types' ;
$plural = 'Custom Posts (Types)' ;
$labels = array(
'name' => __( $name ),
'singular_name' => __( $singular ),
'add_new' => _x( 'Add New', $singular ),
'add_new_item' => __( 'Add New' . $singular ),
'edit_item' => __( 'Edit' . $singular ),
'new_item' => __( 'New' . $singular ),
'view_item' => __( 'View' . $singular ),
'search_items' => __( 'Search' . $name ),
'not_found' => __( 'No' . $name .'Found' ),
'not_found_in_trash' => __( 'No' .$name. 'Found in Trash' ),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'menu_icon' => 'dashicons-format-status',
'public' => false,
'publicly_queryable' => false,
'show_ui' => true,
'query_var' => true,
'capability_type' => 'manage_options',
'hierarchical' => true,
'has_archive' => true,
'show_in_menu' => false,
'exclude_from_search' => true,
'supports' => false,
'rewrite' => array( 'slug' => __( $name )),
);
// See ccli_cpt_testimonials
register_post_type(__( 'custom-post-type-name' ), $args);
}
add_action( 'init', 'register_my_cpt_name' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment