File: /var/www/html/triad-cyber/wp-content/themes/triadcyber/functions.php
<?php
add_theme_support('post-thumbnails');
function enqueue_fancybox() {
wp_enqueue_style('fancybox-css', 'https://cdn.jsdelivr.net/npm/@fancyapps/ui@4.0.27/dist/fancybox.css');
wp_enqueue_script('fancybox-js', 'https://cdn.jsdelivr.net/npm/@fancyapps/ui@4.0.27/dist/fancybox.umd.js', array('jquery'), null, true);
}
add_action('wp_enqueue_scripts', 'enqueue_fancybox');
// Register Custom Post Types
function register_courses_post_types() {
// Main Courses
register_post_type('available_courses', array(
'label' => 'Available Online Courses',
'public' => true,
'menu_position' => 5,
'supports' => array('title', 'editor', 'thumbnail'),
'has_archive' => true,
'show_in_rest' => true,
'rewrite' => array('slug' => 'available_courses', 'with_front' => false),
));
// Sub-Courses
register_post_type('sub_courses', array(
'label' => 'Sub-Courses',
'public' => true,
'menu_position' => 6,
'supports' => array('title', 'editor', 'thumbnail'),
'has_archive' => true,
'show_in_rest' => true,
'rewrite' => array('slug' => 'sub_courses', 'with_front' => false),
));
// Taxonomy to link Sub-Courses to Main Courses
register_taxonomy('course_category', 'sub_courses', array(
'label' => 'Course Category',
'hierarchical' => true,
'show_in_rest' => true,
'rewrite' => array('slug' => 'available_courses', 'with_front' => false),
));
}
add_action('init', 'register_courses_post_types');
// Add Meta Box for Subcourse Images
function subcourse_images_meta_box() {
add_meta_box(
'subcourse_images',
'Subcourse Images',
'subcourse_images_callback',
'sub_courses', // Change this to your sub-course post type slug
'normal',
'high'
);
}
add_action('add_meta_boxes', 'subcourse_images_meta_box');
// Display Meta Box
function subcourse_images_callback($post) {
wp_nonce_field(basename(__FILE__), 'subcourse_images_nonce');
$images = get_post_meta($post->ID, 'subcourse_images', true);
?>
<div id="subcourse_images_container">
<?php if (!empty($images)) : ?>
<?php foreach ($images as $image) : ?>
<div class="subcourse-image">
<img src="<?php echo esc_url($image); ?>" width="150" height="150" />
<input type="hidden" name="subcourse_images[]" value="<?php echo esc_url($image); ?>">
<button type="button" class="remove-image">Remove</button>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<input type="button" id="upload_subcourse_image_button" class="button" value="Add Images" />
<script>
jQuery(document).ready(function($) {
var frame;
$('#upload_subcourse_image_button').on('click', function(e) {
e.preventDefault();
if (frame) {
frame.open();
return;
}
frame = wp.media({
title: 'Select or Upload Images',
button: { text: 'Use these images' },
multiple: true
});
frame.on('select', function() {
var attachments = frame.state().get('selection').toArray();
attachments.forEach(function(attachment) {
var url = attachment.attributes.url;
$('#subcourse_images_container').append(
'<div class="subcourse-image">' +
'<img src="' + url + '" width="150" height="150" />' +
'<input type="hidden" name="subcourse_images[]" value="' + url + '">' +
'<button type="button" class="remove-image">Remove</button>' +
'</div>'
);
});
});
frame.open();
});
$(document).on('click', '.remove-image', function(e) {
e.preventDefault();
$(this).parent().remove();
});
});
</script>
<style>
.subcourse-image {
margin-bottom: 10px;
position: relative;
display: inline-block;
}
.subcourse-image button {
position: absolute;
top: 5px;
right: 5px;
}
</style>
<?php
}
// Save Subcourse Images
function save_subcourse_images($post_id) {
if (!isset($_POST['subcourse_images_nonce']) || !wp_verify_nonce($_POST['subcourse_images_nonce'], basename(__FILE__))) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (isset($_POST['subcourse_images'])) {
update_post_meta($post_id, 'subcourse_images', $_POST['subcourse_images']);
} else {
delete_post_meta($post_id, 'subcourse_images');
}
}
add_action('save_post', 'save_subcourse_images');
// Add Category Column to Subcourses List
function add_subcourses_category_column($columns) {
$columns['course_category'] = 'Category';
return $columns;
}
add_filter('manage_sub_courses_posts_columns', 'add_subcourses_category_column');
// Display Category Name in the Custom Column
function display_subcourses_category_column($column, $post_id) {
if ($column === 'course_category') {
$terms = get_the_terms($post_id, 'course_category'); // Change 'course_category' to your category taxonomy
if ($terms && !is_wp_error($terms)) {
$categories = array();
foreach ($terms as $term) {
$categories[] = $term->name;
}
echo implode(', ', $categories);
} else {
echo 'No Category';
}
}
}
add_action('manage_sub_courses_posts_custom_column', 'display_subcourses_category_column', 10, 2);
function allow_svg_uploads($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'allow_svg_uploads');
function fix_svg_display() {
echo '<style>img[src$=".svg"] { width: 100%; height: auto; }</style>';
}
add_action('admin_head', 'fix_svg_display');
// add_action('wp_head', 'fix_svg_display');
add_filter('wpcf7_form_tag', 'populate_course_select', 10, 2);
function populate_course_select($tag, $unused) {
if ($tag['name'] !== 'course-interested') {
return $tag;
}
$courses = get_posts(array(
'post_type' => 'available_courses', // Replace with your actual post type
'posts_per_page' => -1,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'ASC' // Order by date in descending order
));
$options = array();
if ($courses) {
foreach ($courses as $course) {
$options[] = $course->post_title;
}
}
$tag['raw_values'] = $options; // Set options for the select dropdown
$tag['values'] = $options; // Make sure options are passed to the select tag
return $tag;
}