Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save verygoodplugins/af51ee4b41fd487d398acde9a001bd4a to your computer and use it in GitHub Desktop.
Save verygoodplugins/af51ee4b41fd487d398acde9a001bd4a to your computer and use it in GitHub Desktop.
Modifies WP Fusion's course access permissions for LearnDash so users must be enrolled in the course to see the course page
<?php
// Check course access
function check_ld_access( $can_access, $user_id, $post_id ) {
if ( ! function_exists( 'sfwd_lms_has_access' ) ) {
return $can_access;
}
if ( 'sfwd-courses' == get_post_type( $post_id ) ) {
$settings = get_post_meta( $post_id, 'wpf-settings', true );
if ( true == $settings['lock_content_enrolled'] && false == sfwd_lms_has_access( $post_id, $user_id ) ) {
$can_access = false;
}
}
return $can_access;
}
add_filter( 'wpf_user_can_access', 'check_ld_access', 10, 3 );
// Add the checkbox to the course meta box
function ld_restrict_content_checkbox( $post, $settings ) {
if ( 'sfwd-courses' == $post->post_type ) {
echo '<p>';
echo '<input class="checkbox wpf-restrict-access-checkbox" type="checkbox" name="wpf-settings[lock_content_enrolled]" value="1" ' . checked( $settings['lock_content_enrolled'], 1, false ) . ' />';
echo '<label class="wpf-restrict-access">Users must be enrolled in to view this course</label>';
echo '</p>';
}
}
add_action( 'wpf_meta_box_content', 'ld_restrict_content_checkbox', 12, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment