AI Verified

Name

Woo 404 Redirect Optimizer

Language

PHP

Rating

Voted: 0 by 0 user(s)

Codevault

KasperStuck

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:

15/02/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:

Potential vulnerability found : Weak Cryptographic Hash
Found on line : 8
Code : md5($_SERVER['REQUEST_URI'])

Found 1 vulnerabilities

Woo 404 Redirect Optimizer

 
                    
1// Add a custom function to the 'template_redirect' action
2add_action('template_redirect', function () {
3 // If the current request is not a 404 error, exit the function
4 if (!is_404()) {
5 return;
6 }
7 
8 // Generate a unique cache key for the current request URI
9 $cache_key = 'redirect_' . md5($_SERVER['REQUEST_URI']);
10 
11 // Try to get the redirect URL from the cache
12 $redirect = get_transient($cache_key);
13 
14 // If the redirect URL is not in the cache, calculate it
15 if ($redirect === false) {
16 // Default redirect URL is the home page
17 $redirect = '/';
18 
19 // Split the request URI into segments
20 $paths = explode('/', trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'));
21 
22 // Determine which segment to use based on its existence and non-emptiness
23 if (isset($paths[2]) && !empty($paths[2])) {
24 $path = explode('-', $paths[2]);
25 } else {
26 $path = explode('-', $paths[1]);
27 }
28 
29 // Try to find a product with the given path
30 if ($prod = get_page_by_path($paths[1], OBJECT, 'product')) {
31 $redirect = get_permalink($prod);
32 } else {
33 // If no product is found, try to find a product category with a similar slug
34 while (!empty($path)) {
35 $slug = implode('-', $path);
36 if ($term = get_term_by('slug', $slug, 'product_cat')) {
37 // If a product category is found, set the redirect URL to the category's term link
38 $redirect = get_term_link($term);
39 break;
40 }
41 
42 // Remove the last segment from the path and try again
43 array_pop($path);
44 }
45 }
46 
47 // Cache the calculated redirect URL for 12 hours
48 set_transient($cache_key, $redirect, 12 * HOUR_IN_SECONDS);
49 }
50 
51 // Redirect the user to the calculated URL
52 wp_redirect($redirect);
53 exit();
54});

0

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

0

Remove categories from shop and other pages in Woocommerce

Added: 1 year ago

Last Updated: 1 year ago

WooCommerce

AI Verified

0

WooCommerce Remove Checkout Button From Mini Cart

Added: 1 year ago

Last Updated: 1 year ago

This guide shows you how you can remove the checkout button from the mini cart, forcing users to view their cart before proceeding to the checkout. You may find the button still appears after implemen...

WooCommerce

Pro Verified

0

Remove the WooCommerce Continue Shopping Button

Added: 1 year ago

Last Updated: 1 year ago

Use this to remove the Continue to Shopping Bar and Button if you don't want to show it.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WooCommerce

AI Verified

0

Woo 404 Redirect Optimizer

Added: 7 months ago

Last Updated: 7 months ago

WordPress Admin

AI Verified

0

Multi Site - Run Crons

Added: 7 months ago

Last Updated: 7 months ago

The Multisite Cron Executor is a streamlined WordPress plugin designed for efficient management of WP-Cron jobs across a multisite network. It automates the execution of cron jobs on each site, ensuri...