Skip to content

Instantly share code, notes, and snippets.

@onwp
Forked from weismannweb/ld-auto-mark-complete.php
Created September 9, 2021 21:47
Show Gist options
  • Save onwp/0f8fba8ea204a59af6c620ae14a91ca2 to your computer and use it in GitHub Desktop.
Save onwp/0f8fba8ea204a59af6c620ae14a91ca2 to your computer and use it in GitHub Desktop.
Auto complete learndash topics and lessons
//thanks to https://gist.github.com/sultann/24baa5483b4632c3cf214a8de5648204#file-ld-auto-mark-complete-php-L17 for most of
//this code, i just corrected it to work with ld_lesson_tag to grab the tags as the original version returned empty
//array since it was looking for standard wp tags
function ld_is_tagged($postId) {
//get all learndash tags on post
$tags = wp_get_post_terms($postId,'ld_lesson_tag');
foreach ($tags as $tag) {
//need to check for auto-mark-complete tag exists on this post
if ($tag->name === 'auto-mark-complete') {
return true;
}
}
return false;
}
function ld_auto_mark_complete() {
global $post;
//lets make sure there is a post object first to not have error
if(!is_object($post)){
return;
}
//lets make sure this is a learndash lesson or a topic first (if you want other types add them)
$types_of_posts_to_complete = array('sfwd-lessons','sfwd-topic');
if (!in_array(get_post_type(),$types_of_posts_to_complete) || !is_singular() ){
return;
}
$postId = $post->ID;
//check if it has the tag to auto complete
if (!ld_is_tagged($postId)) {
return;
}
//if we got this far all checks passed and we can not auto complete the post
learndash_process_mark_complete(get_current_user_id(), $postId);
}
add_action('template_redirect', 'ld_auto_mark_complete');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment