// 🔒 UMP Course Access Restriction Logic
add_action('template_redirect', 'check_user_course_access');
function check_user_course_access() {
// Only run on frontend and when viewing a single post/page
if (is_admin() || !is_singular()) return;
global $post;
// Slugs of protected course pages (change these to match your course URLs)
$protected_slugs = ['course-1', 'python-masterclass', 'excel-training'];
// If current page is not one of the protected slugs, allow access
if (!in_array($post->post_name, $protected_slugs)) return;
// Redirect if user is not logged in
if (!is_user_logged_in()) {
wp_redirect(site_url('/course-access-warning')); // Your custom warning page slug
exit;
}
// Get current user and course page ID
$user_id = get_current_user_id();
$post_id = $post->ID;
// Use UMP to check access
if (function_exists('UMP_hasAccess')) {
$has_access = UMP_hasAccess($user_id, $post_id);
if (!$has_access) {
wp_redirect(site_url('/course-access-warning')); // Your custom warning page slug
exit;
}
} else {
// Optional: Log error if UMP is not active
error_log('UMP_hasAccess function not available.');
}
}