Last active
May 27, 2021 11:37
-
-
Save vovadocent/2c9510bd748c6ef1d05252b9034a67cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Shop Custom Permalinks | |
* Remove 'product' slug from Product permalink | |
* Remove 'product-category' slug from Product Category term link | |
* @author vovadevelopenko | |
*/ | |
defined( 'ABSPATH' ) || exit; | |
/** | |
* Remove "product_cat" from product category term link | |
*/ | |
function vd_remove_product_category_link( $termlink, $term, $taxonomy ) { | |
if ( 'product_cat' != $taxonomy ) { | |
return $termlink; | |
} | |
$termlink = str_replace( '/product-category/', '/', $termlink ); | |
return $termlink; | |
} | |
add_filter( 'term_link', 'vd_remove_product_category_link', 10, 3 ); | |
/** | |
* remove "product" from product permalink | |
*/ | |
function vd_remove_product_slug( $post_link, $post, $leavename ) { | |
if ( 'product' != $post->post_type || 'publish' != $post->post_status ) { | |
return $post_link; | |
} | |
$post_link = str_replace( '/product/', '/', $post_link ); | |
return $post_link; | |
} | |
add_filter( 'post_type_link', 'vd_remove_product_slug', 10, 3 ); | |
/** | |
* Fix request when removed products_cat slug | |
*/ | |
function vd_filter_the_request( $vars ) { | |
if ( !empty( $vars[ 'pagename' ] ) || !empty( $vars[ 'category_name' ] ) || !empty( $vars[ 'name' ] ) || !empty( $vars[ 'attachment' ] ) ) { | |
$slug = !empty( $vars[ 'pagename' ] ) ? $vars[ 'pagename' ] : (!empty( $vars[ 'name' ] ) ? $vars[ 'name' ] : (!empty( $vars[ 'category_name' ] ) ? $vars[ 'category_name' ] : $vars[ 'attachment' ] ) ); | |
// try to find shop Category/Product by existing slug | |
if ( !empty( $pcat_vars = vd_maybe_get_prod_category_var( $slug ) ) ) { | |
$old_vars = $vars; | |
$vars = $pcat_vars; | |
} | |
if ( !empty( $old_vars[ 'paged' ] ) || !empty( $old_vars[ 'page' ] ) ) { | |
$vars[ 'paged' ] = !empty( $old_vars[ 'paged' ] ) ? $old_vars[ 'paged' ] : $old_vars[ 'page' ]; | |
} | |
} elseif ( isset( $vars[ 'error' ] ) ) { | |
// try to find shop Category/Product if none slug is found | |
if ( !empty( $pcat_vars = vd_maybe_get_prod_category_var() ) ) { | |
$vars = $pcat_vars; | |
} | |
} | |
return $vars; | |
} | |
add_filter( 'request', 'vd_filter_the_request', 20, 1 ); | |
/** | |
* Try to find product category by Request URI / slug | |
*/ | |
function vd_maybe_get_prod_category_var( $slug = null ) { | |
global $wpdb; | |
if ( empty( $slug ) && empty( $slug_arr = vd_request_uri_slug() ) ) | |
return false; | |
if ( is_array( $slug_arr ) ) { | |
$slug = $slug_arr[ 'slug' ]; | |
} | |
// try to find category | |
$sql = "SELECT t.term_id FROM $wpdb->terms t | |
LEFT JOIN $wpdb->term_taxonomy tt ON tt.term_id = t.term_id | |
WHERE tt.taxonomy = 'product_cat' AND t.slug = '$slug'"; | |
$cat = $wpdb->get_var( $sql ); | |
if ( $cat ) { | |
return array( | |
'product_cat' => $slug, | |
'paged' => isset( $slug_arr ) ? $slug_arr[ 'page' ] : '', | |
); | |
} | |
// try to find product | |
$sql = "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'product' AND post_name = '$slug'"; | |
$prod = $wpdb->get_var( $sql ); | |
if ( $prod ) { | |
return array( | |
'product' => $slug, | |
'name' => $slug, | |
'post_type' => 'product', | |
'page' => '', | |
); | |
} | |
return false; | |
} | |
/** | |
* Get slug from request URI | |
* @return array | |
*/ | |
function vd_request_uri_slug() { | |
$page = $slug = ''; | |
$uri_parts = parse_url( $_SERVER[ 'REQUEST_URI' ] ); | |
if ( empty( $uri_parts[ 'path' ] ) || empty( $uri = trim( $uri_parts[ 'path' ], '/' ) ) ) | |
return false; | |
if ( strpos( $uri, '/page/' ) !== false ) { | |
$uri_arr = explode( '/page/', $uri ); | |
$uri = $uri_arr[ 0 ]; | |
$page = intval( $uri_arr[ 1 ] ); | |
} | |
$slug_arr = explode( '/', $uri ); | |
$slug = $slug_arr[ count( $slug_arr ) - 1 ]; | |
return array( | |
'slug' => $slug, | |
'page' => $page, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment