Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

LearnDash - autocomplete topic without page load WordPress

  • SOLVED

Hi - I'm using the LearnDash plugin for our training website.

The course is split up into 7 lessons, each with 3 or 4 topics.

In each lesson I want one of the topics to be autocompleted - without the user having to visit the topic.

I have found code here that will autocomplete on page load when the topic has a specific tag:

https://gist.github.com/sultann/24baa5483b4632c3cf214a8de5648204

I want specific posts (topics) to be marked complete for every user without page load.

Answers (3)

2017-03-07

Francisco Javier Carazo Gil answers:

Good morning,

Which is the custom post type of the lessons? With this we can do it.

Something like:


$args = array( 'post_type' => 'lesson', 'posts_per_page' => -1, 'post_status' => 'any' );
$lessons = get_posts( $args );

foreach( $lessons as $lesson ){
if( has_term( 'auto-mark-complete', 'the_taxonomy', $lesson ) )
learndash_process_mark_complete( $user, $lesson->ID);
}


The problem:
* You have to fill the taxonomy in the has_term order
* We should know which users can autocomplete each lesson


Austin comments:

Hi - the post type is: sfwd-topic

I changed the slug with a function to: action-item


Francisco Javier Carazo Gil comments:

Ok so it would be:

$args = array( 'post_type' => 'sfwd-topic', 'posts_per_page' => -1, 'post_status' => 'any' );

We need to know now:
* The taxonomy of the tags in LearnDash
* The list of users to autocomplete in each lesson (if they are all we need a function to retrieve all users from a lesson)


Francisco Javier Carazo Gil comments:

Respect to page load don't worry you can add it to functions.php for example.


Austin comments:

Hi - I can't tell what taxonomy the terms are in - it just says 'Tags'. How would I find out?

It has to work for ALL users - I don't want to update a list of user_ids every time I add a new user.


Francisco Javier Carazo Gil comments:

$args = array( 'post_type' => 'lesson', 'posts_per_page' => -1, 'post_status' => 'any' );
$lessons = get_posts( $args );

foreach( $lessons as $lesson ){
if( has_term( 'auto-mark-complete', 'the_taxonomy', $lesson ) ){
$users = get_users();
foreach( $users as $user )
learndash_process_mark_complete( $user->ID, $lesson->ID);
}
}

You can see the taxonomy in the slug in the URL of that tags.


Austin comments:

Where does the code go?


Francisco Javier Carazo Gil comments:

For example in functions.php inside a hook:

add_action('wp', 'cod_autocomplete');

function cod_autocomplete(){
// here the code
}


Austin comments:

Hi - nothing happens when I add the code:

add_action('wp', 'cod_autocomplete');

function cod_autocomplete(){
$args = array( 'post_type' => 'sfwd-topic', 'posts_per_page' => -1, 'post_status' => 'any' );
$lessons = get_posts( $args );

foreach( $lessons as $lesson ){
if( has_term( 'auto-mark-complete', 'tag', $lesson ) ){
$users = get_users();
foreach( $users as $user )
learndash_process_mark_complete( $user->ID, $lesson->ID);
}
}
}


Austin comments:

And I added the tag to the post: auto-mark-complete


Francisco Javier Carazo Gil comments:

Austin,

Change to this:

add_action('admin_footer', 'cod_autocomplete');

function cod_autocomplete(){
$args = array( 'post_type' => 'sfwd-topic', 'posts_per_page' => -1, 'post_status' => 'any' );
$lessons = get_posts( $args );

foreach( $lessons as $lesson ){
var_dump( "Working with lesson: " . $lesson->ID );
if( has_term( 'auto-mark-complete', 'tag', $lesson ) ){
$users = get_users();
foreach( $users as $user )
var_dump( "Autocomplete user: " . $user->ID);
learndash_process_mark_complete( $user->ID, $lesson->ID);
}
}
}


Austin comments:

Nope, no change.


Francisco Javier Carazo Gil comments:

Could you tell me what appears in the end of the wp-admin?


Austin comments:

I prefer not to have code appear in the admin footer.

I'm getting code like this:

"Working with lesson: 4327" string(25) "Working with lesson: 3970" string(25) "Working with lesson: 3741" string(25) "Working with lesson: 3064" string(25) "Working with lesson: 2522" string(25) "Working with lesson: 2521" string(25) "Working with lesson: 2520" string(25) "Working with lesson: 2519" string(25) "Working with lesson: 2518" string(25) "Working with lesson: 2517" string(25) "Working with lesson: 2516" string(25)


Francisco Javier Carazo Gil comments:

Ok so the problem is that something here is bad:

if( has_term( 'auto-mark-complete', 'tag', $lesson ) ){

Maybe it can be 'tag'.


Francisco Javier Carazo Gil comments:

Check for the correct taxonomy please!


Austin comments:

Hi - this is the URL for the archive (domain replaced):

http://www.mydomain.com/wp-admin/edit.php?post_type=sfwd-topic&tag=auto-mark-complete


Austin comments:

I added a new custom taxonomy for the CPT - it didn't work:

if( has_term( 'auto-mark-complete', 'topicstatus', $lesson ) ){


Francisco Javier Carazo Gil comments:

Try this:

if( has_term( 'auto-mark-complete', 'tag', $lesson->ID ) ){

Anyway I was out and I have to go out again. Later in 2:30 hours I will be here, if you want I can check it directly in your site.

We are doing something wrong.


Austin comments:

Hi - I tried that - no luck.

I will check back later. Thanks!


Francisco Javier Carazo Gil comments:

Ups, I would need to see what's happening.

Sorry but from here I cannot do more.

If you want send me a private message and we talk.


Austin comments:

Hi - I can't see how to send a private message.


Francisco Javier Carazo Gil comments:

Oh I cannot see it...

Maybe you can write me carazo AT gmail DOT com when we get the solution we come back here to show which was the problem.


Austin comments:

Hi Francisco - I have received working code below.


Francisco Javier Carazo Gil comments:

Ok perfect.

Please take care of our effort anyway.

2017-03-07

Arnav Joy answers:

what will criteria of your auto completion like the code you have showed uses tag .. what you will use ??
post id ??


Austin comments:

I can add the post ids to a function - my main concern is that it does not require page load.


Arnav Joy comments:

I think you can use this code ..
add this code in functions.php and just refresh any page or admin it will execute and will change for all users .. and then you can remove this code ..
Please note that this code uses same tag method of changing status as shown by you.
please take backup of db before doing it..


function ld_is_topic($postId) {
$tags = wp_get_post_tags($postId);
foreach ($tags as $tag) {
if ($tag->name === 'auto-mark-complete') {
return true;
}
}
return false;
}

function ld_auto_mark_complete() {
global $post;
$postId = $post->ID;
if (!ld_is_topic($postId)) {
return;
}

$blogusers = get_users();

foreach ( $blogusers as $user )
learndash_process_mark_complete($user->ID, $postId);
}

add_action('wp', 'ld_auto_mark_complete');


Austin comments:

Hi - would I leave the code in functions.php do it works for for any new users I add?


Arnav Joy comments:

yes if you don't delete this code.. but please check it once and take proper backup of db.


Austin comments:

Hi - I get an HTTP ERROR 500


Arnav Joy comments:

how many users you have ?


Austin comments:

224


Arnav Joy comments:

are you getting any error in error log?


Austin comments:

Nope - nothing in there.

2017-03-07

Rempty answers:

Hello Austin
i really suggest don't do this to all users each time any page is refreshed
Why? because will take resources from server, maybe a solution is check if the user visit X page and update only when visit the page.

This code will check if current is logged in and if visit X page, if yes will update to complete the topics in an array

function ld_auto_mark_complete() {
global $post;
$postId = $post->ID;
//ID of page X
$yourpage_id=123;
if ($postId==$yourpage_id && is_user_logged_in()) {
//arrray of ids of topics
$array_of_lessons_ids=array(123,345,678);
foreach($array_of_lessons_ids as $idl){
learndash_process_mark_complete(get_current_user_id(), $idl);
}
}
}
add_action('template_redirect', 'ld_auto_mark_complete');

But at the end if you want to do the check in every page (remember this only will autocomplete the topics of the current logged in user)

function ld_auto_mark_complete() {
if (is_user_logged_in()) {
//arrray of ids of topics
$array_of_lessons_ids=array(123,345,678);
foreach($array_of_lessons_ids as $idl){
learndash_process_mark_complete(get_current_user_id(), $idl);
}
}
}
add_action('template_redirect', 'ld_auto_mark_complete');


//The code should be added to functions.php
You can send me a PM if have problems.


Austin comments:

Hi - that works great, thank you!

I've used the post id of the first topic that the user has to visit to trigger autocomplete for the other topics in the array.