Skip to content

Instantly share code, notes, and snippets.

@weismannweb
Created November 9, 2018 21:35
Show Gist options
  • Save weismannweb/7fa8a5e497dfd4c892fe05803933a8ea to your computer and use it in GitHub Desktop.
Save weismannweb/7fa8a5e497dfd4c892fe05803933a8ea 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');
@jjjolley
Copy link

Hello,
thanks for this.
Do you know of a way to redirect "ALL" lessons to the first topic in Learndash?

Like:
Lesson 1 - rediect to topic 1 and will still complete lesson 1 while going to the first topic.

I'm trying to cut down on plugins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment