/** * 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 ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; foreach ($assignments as $assignment) { $author_name = get_the_author_meta('display_name', $assignment->post_author); $publish_date = get_the_date('Y-m-d', $assignment->ID); $file_name = get_post_meta($assignment->ID, 'file_name', true); $file_link = get_post_meta($assignment->ID, 'file_link', true); // Trim the title to a maximum of 16 characters $trimmed_title = mb_strimwidth($assignment->post_title, 0, 19, '...'); // Trim the filename to a maximum of 25 characters $trimmed_filename = mb_strimwidth($file_name, 0, 25, '...'); echo ''; echo ''; echo ''; echo ''; // Check if both file_name and file_link are available if ($file_name && $file_link) { $file_link = esc_url($file_link); echo ''; } else { echo ''; // Empty cell if file information is not available } echo ''; } echo ''; echo '
TitleAuthorPublish DateAssignment Name
' . esc_html($trimmed_title) . '' . esc_html($author_name) . '' . esc_html($publish_date) . '' . esc_html($trimmed_filename) . '
'; } else { echo '

No SFWD assignments found.

'; } return ob_get_clean(); } add_shortcode('sfwd_assignment_list', 'sfwd_assignment_list_shortcode');