last_known_learndash_post_types
Adding wp_head action so that we capture the type of post / page user is on and add that to wordpress options table. Filters the array of LearnDash post types to include in the last known post type tracking.
add_filter( 'last_known_learndash_post_types', $callback, 10, 1 );
Description
This filter allows developers to modify the array of LearnDash post types that are considered when tracking the user's last known page. It's applied when determining which content types to save to WordPress options for resuming user progress. Developers can add or remove post types from this list to customize resume functionality.
Usage
add_filter( 'last_known_learndash_post_types', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Example of filtering the LearnDash post types for the last known page.
* This function might be used to exclude certain LearnDash post types
* from being considered as "last known pages" for resuming content.
* For instance, if you have a custom LearnDash integration that uses a
* new post type, you might want to add it here. Or if you want to
* remove a specific type from this functionality.
*
* @param array $learndash_post_types The array of LearnDash post types.
* @return array The modified array of LearnDash post types.
*/
function my_custom_learndash_post_types( $learndash_post_types ) {
// Example: Remove 'sfwd-quiz' from being considered a last known page type.
// This means users won't be able to resume from a quiz directly.
$key_to_remove = array_search( 'sfwd-quiz', $learndash_post_types, true );
if ( false !== $key_to_remove ) {
unset( $learndash_post_types[ $key_to_remove ] );
}
// Example: Add a hypothetical custom LearnDash post type if it exists.
// Assuming 'my-custom-ld-module' is a custom post type integrated with LearnDash.
if ( post_type_exists( 'my-custom-ld-module' ) ) {
$learndash_post_types[] = 'my-custom-ld-module';
}
// Always return the modified array.
return $learndash_post_types;
}
add_filter( 'last_known_learndash_post_types', 'my_custom_learndash_post_types', 10, 1 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/learn-dash-resume.php:150
src/classes/learn-dash-resume.php:356
public static function find_last_known_learndash_page() {
$user = wp_get_current_user();
if ( is_user_logged_in() ) {
/* declare $post as global so we get the post->ID of the current page / post */
global $post;
// Sanity check page doesn't exist
if ( ! is_object( $post ) ) {
return;
}
/* Limit the plugin to LearnDash specific post types */
$learn_dash_post_types = apply_filters(
'last_known_learndash_post_types',
array(
'sfwd-courses',
'sfwd-lessons',
'sfwd-topic',
'sfwd-quiz',
'sfwd-certificates',
'sfwd-assignment',
)
);
$step_id = $post->ID;
$step_course_id = learndash_get_course_id( $step_id );
if ( empty( $step_course_id ) ) {
$step_course_id = 0;
}
if ( is_singular( $learn_dash_post_types ) ) {
update_user_meta( $user->ID, 'learndash_last_known_page', $step_id . ',' . $step_course_id );
if ( 'sfwd-courses' !== $post->post_type ) {
update_user_meta( $user->ID, 'learndash_last_known_course_' . $step_course_id, $step_id );
}
}
}
}