Browse: Home / Snippets /

Restrict access to certificates

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.

// Example of restricting access to the LearnDash Certificates. In the example below 
// only the admin ( users with 'manage_options' capability ) can access certificates. 
add_action('template_redirect', function(){
	$post_type = get_query_var( 'post_type' );
	if ( $post_type == 'sfwd-certificates' ) {
		// CHANGE 'manage_options' TO ANY USER CAPABILITY TO CHECK
		if ( ( !is_user_logged_in() ) || ( !current_user_can( 'manage_options' ) ) ) {
			// If the post_type is certiicate 
			// and the user is either not logged in or not admin ('manage_options') 
			// then redirect to home.
			wp_redirect( home_url() );
			exit;
		}
	}
});