LMS & eLearning

Snippet: Auto-Approve LearnDash Assignments From Trusted Groups

Approve assignment uploads automatically for selected groups while keeping manual review elsewhere: the upload action, learndash_approve_assignment_by_id, points, and the built-in auto-approve setting.

Snippet: Auto-Approve LearnDash Assignments From Trusted Groups

LearnDash lessons and topics can require an assignment upload before the step can be completed, and each lesson has a switch to approve uploads automatically or hold them for review. The switch is per lesson, not per learner. Sites that train both external customers and internal staff often want the same lesson to auto-approve for staff, whose work is checked elsewhere, while customers wait for an instructor. LearnDash 4.18.0.1 fires an action after every upload and provides the function the admin's Approve button calls, so group-based auto-approval is a short snippet. This article explains the moving parts and the consequences for points and progression.

The built-in setting first

On the lesson or topic's Settings tab, Assignment Uploads has an Auto-Approve toggle. When it is on, uploads are approved as they arrive and the step can be marked complete immediately; when off, the assignment post waits with an approval status of pending until someone approves it under LearnDash LMS > Assignments. The snippet below only matters for lessons where that toggle is off; it adds a second path to approval for selected learners.

The hook and the approval function

In includes/ld-assignment-uploads.php, after the file is saved and the assignment post created, LearnDash fires do_action( 'learndash_assignment_uploaded', $assignment_post_id, $assignment_meta ). The meta array includes user_id, lesson_id, course_id, lesson_title, lesson_type, file_path and a few bookkeeping fields. The same file defines learndash_approve_assignment_by_id( $assignment_id ), which reads the author and lesson from the post and calls learndash_approve_assignment( $user_id, $lesson_id, $assignment_post_id ), the function behind the admin's Approve action. That function runs the learndash_approve_assignment filter, marks the assignment approved, and marks the lesson or topic complete for the learner when the lesson is set to be completed by assignment approval.

The snippet

<?php
/**
 * Group IDs whose members' assignments are approved on upload.
 */
function wwc_auto_approve_groups() {
    return array( 321, 654 );
}

add_action(
    'learndash_assignment_uploaded',
    function ( $assignment_post_id, $assignment_meta ) {
        $user_id   = isset( $assignment_meta['user_id'] ) ? (int) $assignment_meta['user_id'] : 0;
        $lesson_id = isset( $assignment_meta['lesson_id'] ) ? (int) $assignment_meta['lesson_id'] : 0;

        if ( ! $user_id || ! $lesson_id ) {
            return;
        }

        // Already approved by the lesson's own auto-approve setting.
        if ( learndash_is_assignment_approved( $assignment_post_id ) ) {
            return;
        }

        $trusted = false;
        foreach ( wwc_auto_approve_groups() as $group_id ) {
            if ( learndash_is_user_in_group( $user_id, $group_id ) ) {
                $trusted = true;
                break;
            }
        }

        if ( ! $trusted ) {
            return;
        }

        // Award full points when the lesson uses assignment points.
        $points_enabled = learndash_get_setting( $lesson_id, 'lesson_assignment_points_enabled' );
        $max_points     = (int) learndash_get_setting( $lesson_id, 'lesson_assignment_points_amount' );
        if ( 'on' === $points_enabled && $max_points > 0 ) {
            update_post_meta( $assignment_post_id, 'points', $max_points );
        }

        learndash_approve_assignment_by_id( $assignment_post_id );
    },
    10,
    2
);

learndash_is_assignment_approved() short-circuits lessons whose own toggle is on, so the snippet never approves twice. Points are written before approval so that the learner's record shows the award; if the lesson does not use assignment points the block is skipped. Group membership is checked with learndash_is_user_in_group( $user_id, $group_id ), which reads the cached group membership.

What approval triggers

  • Completion. If the lesson is set to require assignment approval for completion, learndash_approve_assignment() marks the step complete, which can cascade to course completion and certificate availability.
  • The learndash_approve_assignment filter. Any code hooked there, including this site's other customisations, runs for automatic approvals too. Return false from it to veto an approval under some condition.
  • Emails. LearnDash's own assignment emails, if enabled, treat the upload as approved. Group leaders will not see the assignment in their pending list.

Variations

Approve by role instead of group by swapping the loop for a check on get_userdata( $user_id )->roles. Approve only for specific lessons by comparing $lesson_id against a list or a lesson meta flag. Or approve only small files: the meta's file_path points to the upload, and filesize() on the local path lets you route large submissions to manual review. Keep every variation on the same hook so there is one place to read when someone asks why an assignment was approved.

Timing and the upload response

The hook runs inside the upload request, after the file is saved and before the learner's page reloads, so the approval is visible immediately: the lesson shows as complete and the Mark Complete button, if the lesson also requires it, becomes available. The approval function itself is light, a few meta updates and the completion processing, so there is no need to queue it. If you add heavier work to the same hook, such as notifying several people, queue that part and keep the approval inline so the learner's experience is consistent.

Audit trail

Automatic approval should leave a mark. Add update_post_meta( $assignment_post_id, 'wwc_auto_approved', time() ) before the approval call, and show it as a column on the Assignments list so instructors can tell automatic from manual approvals at a glance; adding list columns is covered in our admin columns guide, and the assignment hooks in the assignment and essay hooks reference.

Testing

  1. Create a lesson with uploads required, Auto-Approve off and 10 assignment points, and set it to complete on approval.
  2. Upload as a learner outside the trusted groups: the assignment stays pending and the lesson incomplete.
  3. Upload as a member of a trusted group: the assignment is approved with 10 points and the lesson is complete.
  4. Turn the lesson's Auto-Approve on and upload as the trusted learner again: approved once, with no duplicate processing.

When to get help

A group rule is quick to add. Review workflows with reviewer assignment, rubrics and feedback, or an instructor queue with due dates are projects our LearnDash services team builds, and a LearnDash developer can also reconcile existing pending assignments when a rule changes.

Frequently asked questions

Can LearnDash auto-approve assignments for some learners only?

Not with settings alone; Auto-Approve is per lesson. The learndash_assignment_uploaded action lets code approve selectively, for example by group.

Which function approves an assignment in code?

learndash_approve_assignment_by_id( $assignment_id ), which calls the same learndash_approve_assignment() the admin Approve button uses.

Does automatic approval complete the lesson?

Yes, when the lesson is set to be completed on assignment approval, with the usual cascade to course completion.

How do I award points on automatic approval?

Write the points to the assignment's points post meta before calling the approval function, as the snippet does when the lesson has assignment points enabled.

Add WorldWin Coder to your preferred sources

Add worldwincoder.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.

· Developers and project managers

Articles written and reviewed by the developers and project managers at WorldWin Coder, who build and maintain websites, web applications and mobile apps on WordPress, Moodle, PHP, Laravel, CodeIgniter and Flutter.

Keep reading

Have something to build, fix or move?

Tell us where you are. A developer reads every inquiry, and you get next steps and a free quote within one business day.