Contents
Snippet #
Important: All snippets are provided as-is without support or guarantees. These snippets are provided as guidelines for advanced users looking to customize LearnDash. For any additional help or support with these snippets, we recommend reaching out to a LearnDash Expert.
/* Filter to redirect Course grid link to closed course "Button URL" if set, instead of course page */
add_filter( 'learndash_course_grid_custom_button_link', function($button_link, $post_id ) {
// First, make sure $post_id is a Course.
if ( get_post_type( $post_id ) !== 'sfwd-courses' ) {
return $button_link;
}
// Then, determine if the user is logged in or enrolled in the course.
if ( !is_user_logged_in() || !ld_course_check_user_access( $post_id, get_current_user_id() ) ) {
$course_info = learndash_get_course_price($post_id);
$course_settings = learndash_get_setting($post_id);
// If they are not, determine if the course is closed and has Button URL set
if ($course_info['type'] === 'closed' && $course_settings['custom_button_url']) {
// If it is and does, set $button_link to the Button URL
$button_link = $course_settings['custom_button_url'];
}
}
// Ultimately, return $button_link
return $button_link;
},10,2 );