AI Verified

Name

Restrict-Course-Pages

About

Ever wanted to restrict access to Pages until certain products are purchased? You can even redirect them to a page/post when they try to access prior to logging in.

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

Add your pages to restrict, the product IDs, and the redirect URLs.

Codevault

WebSquadron

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

11/04/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Restrict-Course-Pages

 
                    
1function restrict_page_access() {
2 // Configuration
3 $access_rules = [
4 'course-1' => [
5 'product_ids' => [183],
6 'redirect' => 'https://websquadron.s3-tastewp.com/shop',
7 ],
8 'course-2' => [
9 'product_ids' => [183, 186],
10 'redirect' => 'https://websquadron.s3-tastewp.com/contact',
11 ],
12 'course-3' => [
13 'product_ids' => [190, 191],
14 'redirect' => 'https://websquadron.s3-tastewp.com/home',
15 ],
16 ];
17 
18 // Get current user ID and check if the user is an administrator
19 $user_id = get_current_user_id();
20 $is_admin = current_user_can('administrator');
21 
22 // If user is an administrator, skip restriction checks
23 if ($is_admin) {
24 return;
25 }
26 
27 // Get the current page
28 $current_page = get_queried_object();
29 
30 // Ensure $current_page is a WP_Post object
31 if (!($current_page instanceof WP_Post)) {
32 return;
33 }
34 
35 // Iterate through access rules and apply restrictions
36 foreach ($access_rules as $slug => $rule) {
37 if ($current_page->post_name === $slug) {
38 // Check if user has purchased any of the required products
39 $has_purchased = false;
40 foreach ($rule['product_ids'] as $product_id) {
41 if (wc_customer_bought_product($user_id, $user_id, $product_id)) {
42 $has_purchased = true;
43 break;
44 }
45 }
46 
47 // Redirect if the user hasn't purchased the required products
48 if (!$has_purchased) {
49 wp_redirect($rule['redirect']);
50 exit;
51 }
52 }
53 }
54}
55add_action('template_redirect', 'restrict_page_access');

0

Related Snippets

Please see some snippets below related to this snippet..

WordPress Admin

AI Verified

1

Disable Gutenberg Editor (use Classic Editor)

Added: 1 year ago

Last Updated: 1 year ago

WordPress Admin

AI Verified

0

Show Page Title in WordPress Menu

Added: 2 months ago

Last Updated: 2 months ago

Adding HTML directly into a WordPress menu to dynamically fetch and display the page title is not supported through the WordPress menu editor interface out of the box. However, you can achieve dynamic...

WordPress Admin

Unverified

0

Clamp

Added: 7 months ago

Last Updated: 7 months ago

Fluid Typography Code Snippet - Add the Tool to your Dashboard or Page

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

31

Remove Unused Javascript

Added: 1 year ago

Last Updated: 4 weeks ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

10

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 2 days ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 7 months ago

Last Updated: 1 month ago

This can be used for any WordPress Builder to aid working with CSS Grids.