-
-
Save zorzv/2c8f3f825f1c945e6e0583cc5a7dfd12 to your computer and use it in GitHub Desktop.
Auto complete learndash topics and lessons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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