/**
* LearnDash Custom Dashboard Tab for Student Assignments
*
* This code adds a custom "Assignment" tab to WordPress's LearnDash dashboard navigation menu.
* The tab links to a page with the slug 'student-assignment' and displays an icon and label.
*/
function ld_dashboard_add_nav_menu( $menu_items ) {
$menu_items['all']['custom'] = array(
'url' => get_the_permalink() . '?tab=student-assignment', // tab=custom set the slug of your custom tab, which will later be used to display content in this tab.
'icon' => '',
'label' => 'Assignment',
);
return $menu_items;
}
add_filter( 'learndash_dashboard_nav_menu', 'ld_dashboard_add_nav_menu', 10, 1 );
function ld_dashboard_after_content_callback() {
if ( isset( $_GET['tab'] ) && 'student-assignment' === $_GET['tab'] ) {
echo do_shortcode('[sfwd_assignment_list]');
}
}
add_action( 'ld_dashboard_after_content', 'ld_dashboard_after_content_callback' );
function sfwd_assignment_list_shortcode() {
ob_start();
$args = array(
'post_type' => 'sfwd-assignment',
'posts_per_page' => -1, // Retrieve all assignments
// Add any additional query parameters as needed
);
// Check if the current user is an administrator
$is_admin = current_user_can('manage_options');
// If the user is an admin, show all posts; otherwise, show only their own posts
if (!$is_admin) {
$args['author'] = get_current_user_id();
}
$assignments = get_posts($args);
if ($assignments) {
echo '
| Title | '; echo 'Author | '; echo 'Publish Date | '; echo 'Assignment Name | '; echo '|
|---|---|---|---|---|
| ' . esc_html($trimmed_title) . ' | '; echo '' . esc_html($author_name) . ' | '; echo '' . esc_html($publish_date) . ' | '; // Check if both file_name and file_link are available if ($file_name && $file_link) { $file_link = esc_url($file_link); echo '' . esc_html($trimmed_filename) . ' | '; } else { echo ''; // Empty cell if file information is not available } echo ' |
No SFWD assignments found.
'; } return ob_get_clean(); } add_shortcode('sfwd_assignment_list', 'sfwd_assignment_list_shortcode');