Imagine if you could access your WooCommerce product page using a shorter and more engaging URL like example.com/sku00001 instead of the lengthy permalink example.com/shop/t-shirts/t-shirt-casual-v-neck.

In this snippet, we’ll create a simple redirect that maps a custom URL containing the product SKU to the corresponding product page. This way, you can use a concise and attractive URL in your email marketing, display advertising, blog posts, and anywhere you want to share a “pretty link” instead of a lengthy product page URL.

Enjoy simplifying your links!

function fixelar_redirect_sku_in_url_to_product() {
   if ( is_404() && ( $sku = $GLOBALS['wp']->request ) && ( $id = wc_get_product_id_by_sku( $sku ) ) ) {
      wp_redirect( get_permalink( $id ) );
      exit;
   }
}
add_action( 'template_redirect', 'fixelar_redirect_sku_in_url_to_product' );