Advertisement
vapvarun

LearnDash Custom Dashboard Tab for Student Assignments

Nov 28th, 2023
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.40 KB | Software | 0 0
  1. /**
  2.  * LearnDash Custom Dashboard Tab for Student Assignments
  3.  *
  4.  * This code adds a custom "Assignment" tab to WordPress's LearnDash dashboard navigation menu.
  5.  * The tab links to a page with the slug 'student-assignment' and displays an icon and label.
  6.  */
  7.  
  8. function ld_dashboard_add_nav_menu( $menu_items ) {
  9.     $menu_items['all']['custom'] = array(
  10.         '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.
  11.         'icon'  => '<img src="https://demos.wbcomdesigns.com/wp-content/uploads/2023/11/report.svg">',
  12.         'label' => 'Assignment',
  13.     );
  14.     return $menu_items;
  15. }
  16. add_filter( 'learndash_dashboard_nav_menu', 'ld_dashboard_add_nav_menu', 10, 1 );
  17.  
  18. function ld_dashboard_after_content_callback() {
  19.     if ( isset( $_GET['tab'] ) && 'student-assignment' === $_GET['tab'] ) {
  20.         echo do_shortcode('[sfwd_assignment_list]');
  21.     }
  22. }
  23.  
  24. add_action( 'ld_dashboard_after_content', 'ld_dashboard_after_content_callback' );
  25.  
  26.  
  27. function sfwd_assignment_list_shortcode() {
  28.     ob_start();
  29.  
  30.     $args = array(
  31.         'post_type'      => 'sfwd-assignment',
  32.         'posts_per_page' => -1, // Retrieve all assignments
  33.         // Add any additional query parameters as needed
  34.     );
  35.  
  36.     // Check if the current user is an administrator
  37.     $is_admin = current_user_can('manage_options');
  38.  
  39.     // If the user is an admin, show all posts; otherwise, show only their own posts
  40.     if (!$is_admin) {
  41.         $args['author'] = get_current_user_id();
  42.     }
  43.  
  44.     $assignments = get_posts($args);
  45.  
  46.     if ($assignments) {
  47.         echo '<table>';
  48.         echo '<thead>';
  49.         echo '<tr>';
  50.         echo '<th>Title</th>';
  51.         echo '<th>Author</th>';
  52.         echo '<th>Publish Date</th>';
  53.         echo '<th>Assignment Name</th>';
  54.         echo '</tr>';
  55.         echo '</thead>';
  56.         echo '<tbody>';
  57.  
  58.         foreach ($assignments as $assignment) {
  59.             $author_name = get_the_author_meta('display_name', $assignment->post_author);
  60.             $publish_date = get_the_date('Y-m-d', $assignment->ID);
  61.             $file_name = get_post_meta($assignment->ID, 'file_name', true);
  62.             $file_link = get_post_meta($assignment->ID, 'file_link', true);
  63.  
  64.             // Trim the title to a maximum of 16 characters
  65.             $trimmed_title = mb_strimwidth($assignment->post_title, 0, 19, '...');
  66.  
  67.             // Trim the filename to a maximum of 25 characters
  68.             $trimmed_filename = mb_strimwidth($file_name, 0, 25, '...');
  69.  
  70.             echo '<tr>';
  71.             echo '<td>' . esc_html($trimmed_title) . '</td>';
  72.             echo '<td>' . esc_html($author_name) . '</td>';
  73.             echo '<td>' . esc_html($publish_date) . '</td>';
  74.  
  75.             // Check if both file_name and file_link are available
  76.             if ($file_name && $file_link) {
  77.                 $file_link = esc_url($file_link);
  78.                 echo '<td><a href="' . $file_link . '" target="_blank">' . esc_html($trimmed_filename) . '</a></td>';
  79.             } else {
  80.                 echo '<td></td>'; // Empty cell if file information is not available
  81.             }
  82.  
  83.             echo '</tr>';
  84.         }
  85.  
  86.         echo '</tbody>';
  87.         echo '</table>';
  88.     } else {
  89.         echo '<p>No SFWD assignments found.</p>';
  90.     }
  91.  
  92.     return ob_get_clean();
  93. }
  94.  
  95. add_shortcode('sfwd_assignment_list', 'sfwd_assignment_list_shortcode');
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement