File: /var/www/linde/wp-content/plugins/lindevr-site-plugin/cpt.php
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
// register Brands taxonomy
add_action( 'init', function() {
register_taxonomy( 'linde_module', 'linde_course', array(
'labels' => array(
'name' => __('Modules', 'linde'),
'singular_name' => __('Module', 'linde'),
'add_new_item' => __('Add New Module', 'linde'),
'edit_item' => __('Edit Module', 'linde'),
'new_item' => __('New Module', 'linde'),
'view_item' => __('View Module', 'linde'),
'view_items' => __('View Module', 'linde'),
'search_items' => __('Search Module', 'linde'),
'not_found' => __('No Modules found', 'linde'),
'all_items' => __('All Modules', 'linde')
),
'rewrite' => array('slug' => 'courses'),
'hierarchical' => false,
));
register_post_type('linde_course',
array(
'labels' => array(
'name' => __('Courses', 'linde'),
'singular_name' => __('Course', 'linde'),
),
'public' => true,
'show_in_menu' => true,
'supports' => array( 'title', 'thumbnail', 'editor' ),
'has_archive' => true,
'rewrite' => array('slug' => 'courses/%cat%'),
));
register_post_type('linde_faq',
array(
'labels' => array(
'name' => __('FAQs', 'linde'),
'singular_name' => __('FAQ', 'linde'),
),
'public' => true,
'show_in_rest' => true,
'show_in_menu' => true,
'supports' => array( 'title', 'thumbnail', 'editor' ),
'has_archive' => true,
'rewrite' => array('slug' => 'faq'),
'taxonomies' => array('post_tag'),
));
});
add_filter( 'manage_linde_course_posts_columns', 'set_custom_edit_linde_course_columns');
function set_custom_edit_linde_course_columns($columns) {
$temp = $columns['date'];
unset( $columns['date'] );
$columns['order'] = 'Session number';
$columns['module'] = 'Module';
$columns['date'] = $temp;
return $columns;
}
add_action( 'manage_linde_course_posts_custom_column' , 'custom_session_number_column', 10, 2 );
function custom_session_number_column( $column, $post_id ) {
if($column == 'order' ) {
echo get_post_meta( $post_id , 'session_number' , true );
} else if ( $column == 'module' ) {
$terms = get_the_terms( $post_id, 'linde_module' );
if ($terms && is_array($terms)) {
$term = array_pop($terms);
echo $term->name;
} else {
echo 'Module not selected';
}
}
}
function change_link( $post_link, $id = 0 ) {
$post = get_post( $id );
if( $post->post_type == 'linde_course' ) {
if ( is_object( $post ) ) {
$terms = wp_get_object_terms( $post->ID, array('linde_module') );
if ( $terms ) {
return str_replace( '%cat%', $terms[0]->slug, $post_link );
}
}
}
return $post_link ;
}
add_filter( 'post_type_link', 'change_link', 1, 3 );
function generated_rewrite_rules() {
add_rewrite_rule(
'^courses/(.*)/(.*)/?$',
'index.php?post_type=linde_course&name=$matches[2]',
'top'
);
}
add_action( 'init', 'generated_rewrite_rules' );
?>