Shorthand Register Functions
-
I gotta get back to work so I’ll make this brief. I just made a function to shorthand register_post_type(). It’d be super nice to have this kind of thing native. I’m not finished making it all encompassing yet, but here’s my comparison.
Shorthand:
register_post_type_easy('newsletters', 'dashicons-email-alt', array('label' => array('newsletter', 'newsletters'), 'public', 'archive', 'exclude_from_search'));
Non-Shorthand
register_post_type('newsletters', array( 'labels' => array( 'name' => 'Newsletters', 'singular_name' => 'Newsletter', 'add_new_item' => 'Add New Newsletter', 'edit_item' => 'Edit Newsletter', 'new_item' => 'New Newsletter', 'view_item' => 'View Newsletters', 'search_items' => 'Search Newsletters', 'not_found' => 'No Newsletters Found', 'not_found_in_trash' => 'No Newsletters found in Trash', 'menu_name' => 'Newsletters' ), 'hierarchical' => false, 'supports' => array('title', 'editor', 'revisions'), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 20, 'menu_icon' => 'dashicons-email-alt', 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => true, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'rewrite' => true, 'capability_type' => 'post' ));
My Function:
function register_post_type_easy($SLUG, $ICON, $PARAMS) { $SLUG = preg_replace('/\s/', '', strToLower($SLUG)); $PARAMS['label'][0] = UcWords(strToLower($PARAMS['label'][0])); $PARAMS['label'][1] = UcWords(strToLower($PARAMS['label'][1])); register_post_type($SLUG, array( 'labels' => array( 'name' => $PARAMS['label'][1], 'singular_name' => $PARAMS['label'][0], 'add_new_item' => sprintf('Add New %s', $PARAMS['label'][0]), 'edit_item' => sprintf('Edit %s', $PARAMS['label'][0]), 'new_item' => sprintf('New %s', $PARAMS['label'][0]), 'view_item' => sprintf('View %s', $PARAMS['label'][1]), 'search_items' => sprintf('Search %s', $PARAMS['label'][1]), 'not_found' => sprintf('No %s Found', $PARAMS['label'][1]), 'not_found_in_trash' => sprintf('No %s found in Trash', $PARAMS['label'][1]), 'menu_name' => (in_array('label', $PARAMS) && isset($PARAMS['label'][2])) ? $PARAMS['label'][2] : UcWords($SLUG) ), 'hierarchical' => (in_array('hierarchical', $PARAMS)) ? true : false, 'supports' => (in_array('thumbnail', $PARAMS)) ? array('title', 'editor', 'revisions', 'thumbnail') : array('title', 'editor', 'revisions'), 'public' => (in_array('public', $PARAMS)) ? true : false, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 20, 'menu_icon' => $ICON, 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => (in_array('exclude_from_search', $PARAMS)) ? true : false, 'has_archive' => (in_array('archive', $PARAMS)) ? true : false, 'query_var' => true, 'can_export' => true, 'rewrite' => true, 'capability_type' => 'post' )); }
- The topic ‘Shorthand Register Functions’ is closed to new replies.