HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux wordpress-ubuntu-s-2vcpu-4gb-fra1-01 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
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' ); 




?>