Skip to content

Instantly share code, notes, and snippets.

@tankbar
Last active October 24, 2017 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tankbar/e54b36592097bdd00c9e17971e859e8b to your computer and use it in GitHub Desktop.
Save tankbar/e54b36592097bdd00c9e17971e859e8b to your computer and use it in GitHub Desktop.
Tankbar Custom Post Type Registration
<?php
function register_programs_post_type(){
register_post_type('program', array(
'labels' => array(
'name' => __('Programs','tankbar'),
'singular_name' => __('Program','tankbar'),
'add_new' => __('Add program','tankbar'),
'add_new_item' => __('Add new program', 'tankbar'),
'edit_item' => __('Edit program','tankbar'),
'new_item' => __('New program','tankbar'),
'view_item' => __('Show program','tankbar'),
'search_items' => __('Search program','tankbar'),
'not_found' => __('No program','tankbar'),
'not_found_in_trash' => __('No programs in trash','tankbar'),
'parent_item_colon' => '',
),
'singular_label' => 'Programs',
'public' => true,
'exclude_from_search' => false,
'show_ui' => true,
'menu_icon' => 'dashicons-playlist-video',
'menu_position' => 22,
'capability_type' => 'post',
'hierarchical' => false,
/*'rewrite' => array(
'slug' => 'program',
'with_front' => true,
'feeds' => false,
),*/
'rewrite' => array('slug' => 'traningsaktivitet/%program_categories%'),
'query_var' => false,
'show_in_nav_menus' => false,
'show_in_rest' => true,
'rest_controller_class' => 'WP_REST_Posts_Controller',
'supports' => array('title', 'page-attributes', 'revisions', 'thumbnail')
));
}
add_action('init','register_programs_post_type');
/* CREATE a unique category taxonomy for campaigns */
/* ADD CATEGOIRES SUPPORT */
if ( ! function_exists( 'program_categories' ) ) {
// Register Custom Taxonomy
function program_categories() {
$args = array(
'hierarchical' => true,
'show_admin_column' => true,
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'rewrite' => array( 'slug' => 'traningsaktivitet' ),
// This array of options controls the labels displayed in the WordPress Admin UI
'labels' => array(
'name' => __( 'Classes', 'tankbar' ),
'singular_name' => __( 'Class', 'tankbar' ),
'search_items' => __( 'Search class', 'tankbar' ),
'all_items' => __( 'All classes', 'tankbar' ),
'parent_item' => __( 'Parent', 'tankbar' ),
'parent_item_colon' => __( 'Parent:', 'tankbar' ),
'edit_item' => __( 'Edit class', 'tankbar' ),
'update_item' => __( 'Update class', 'tankbar' ),
'add_new_item' => __( 'Add new class', 'tankbar' ),
'new_item_name' => __( 'New class', 'tankbar' ),
'menu_name' => __( 'Classes', 'tankbar' ),
),
'show_in_rest' => true,
);
register_taxonomy( 'program_categories', array( 'program' ), $args );
}
// Hook into the 'init' action
add_action( 'init', 'program_categories', 0 );
}
//This part is only if you want a custom URL structure for your CPT
function tankbar_program_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'program_categories' );
if( $terms ){
return str_replace( '%program_categories%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'tankbar_program_post_link', 1, 3 );
?>
<?php
/*
Plugin Name: Tankbar Custom Post Type Registration
Description: Add CPT's for this project
Version: 1.0
Author: Tankbar AB
Author URI: http://www.tankbar.com
Text Domain: tankbar
Domain Path: /languages
*/
load_plugin_textdomain( 'tankbar', false, basename( dirname( __FILE__ ) ) . '/languages/' );
if (!post_type_exists('program') ) {
include_once('programs.php');
}
if (!post_type_exists('facility') ) {
include_once('facility.php');
}
if (!post_type_exists('event') ) {
include_once('events.php');
}
//Disable "Custom fields" menu item in WP-admin
//add_filter('acf/settings/show_admin', '__return_false');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment