"Mark as Complete" Button for Lesson Posts (No LMS Needed)
Adds a [wpide_mark_complete] shortcode that lets logged-in readers tick off a post or page as done, turning a plain blog series into a simple course without an LMS plugin.
Opens WPIDE on
Code
add_action( 'template_redirect', function () {
if ( empty( $_POST['wpide_complete_nonce'] ) || ! is_user_logged_in() ) {
return;
}
$post_id = isset( $_POST['wpide_lesson'] ) ? absint( $_POST['wpide_lesson'] ) : 0;
if ( ! $post_id || ! wp_verify_nonce( sanitize_key( $_POST['wpide_complete_nonce'] ), 'wpide_complete_' . $post_id ) || 'publish' !== get_post_status( $post_id ) ) {
return;
}
$done = array_map( 'intval', (array) get_user_meta( get_current_user_id(), 'wpide_completed_lessons', true ) );
if ( in_array( $post_id, $done, true ) ) {
$done = array_diff( $done, array( $post_id ) );
} else {
$done[] = $post_id;
}
update_user_meta( get_current_user_id(), 'wpide_completed_lessons', array_values( array_unique( $done ) ) );
wp_safe_redirect( get_permalink( $post_id ) );
exit;
} );
add_shortcode( 'wpide_mark_complete', function () {
$post_id = get_the_ID();
if ( ! $post_id ) {
return '';
}
if ( ! is_user_logged_in() ) {
return '<p class="wpide-complete"><a href="' . esc_url( wp_login_url( get_permalink() ) ) . '">Log in</a> to track your progress.</p>';
}
$done = in_array( $post_id, array_map( 'intval', (array) get_user_meta( get_current_user_id(), 'wpide_completed_lessons', true ) ), true );
return '<form method="post" class="wpide-complete' . ( $done ? ' is-done' : '' ) . '">'
. wp_nonce_field( 'wpide_complete_' . $post_id, 'wpide_complete_nonce', true, false )
. '<input type="hidden" name="wpide_lesson" value="' . (int) $post_id . '">'
. '<button type="submit" aria-pressed="' . ( $done ? 'true' : 'false' ) . '">' . ( $done ? '✓ Completed (undo)' : 'Mark as complete' ) . '</button></form>';
} );
Notes
Completed lessons are stored per user in the wpide_completed_lessons user meta. Pair it with the "Series Progress" snippet to show overall progress.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.