Advertisement
sparkweb

FoxyShop: URL Customization

Jan 9th, 2012
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. //FoxyShop URL Customization (FoxyShop 4.2+ required)
  2. //This code will add the product category to the url.
  3. //IMPORTANT: put this in your wp-config.php file:
  4. //define('FOXYSHOP_PRODUCTS_SLUG','products/%foxyshop_categories%');
  5.  
  6. add_filter( 'post_type_link', 'custom_foxyshop_product_link', 10, 3 );
  7.  
  8. function custom_foxyshop_product_link( $permalink, $post, $leavename ) {
  9.     global $wp_query;
  10.     $term_url = '';
  11.     $rewritecode = array(
  12.         '%foxyshop_categories%',
  13.         $leavename ? '' : '%postname%',
  14.     );
  15.     $post_id = $post;
  16.     $post = get_post( $post_id );
  17.  
  18.     // Only applies to Foxyshop products, don't stop on permalinks of other CPTs
  19.     if ($post->post_type != 'foxyshop_product')
  20.         return $permalink;
  21.  
  22.     $permalink_structure = get_option( 'permalink_structure' );
  23.  
  24.     $our_permalink_structure = FOXYSHOP_PRODUCTS_SLUG . "/%postname%/";
  25.  
  26.     if ( '' != $permalink_structure && !in_array( $post->post_status, array( 'draft', 'pending' ) ) ) {
  27.  
  28.         $post_name = $post->post_name;
  29.  
  30.         $product_categories = wp_get_post_terms($post->ID, 'foxyshop_categories');
  31.         $category_name = "";
  32.         foreach($product_categories as $term) {
  33.             $category_name = $term->slug;
  34.  
  35.             if ($term->parent > 0) {
  36.                 $parent_cats = get_ancestors($term->term_id, 'foxyshop_categories');
  37.                 foreach($parent_cats as $parent_id) {
  38.                     $parent_term = get_term_by('id',$parent_id,'foxyshop_categories');
  39.                     $category_name = $parent_term->slug . "/" . $category_name;
  40.                 }
  41.             }
  42.             continue;
  43.         }
  44.  
  45.         if ($category_name == '') $category_name = "uncategorized";
  46.  
  47.         $rewritereplace = array(
  48.             $category_name,
  49.             $post_name
  50.         );
  51.  
  52.         $permalink = str_replace( $rewritecode, $rewritereplace, $our_permalink_structure );
  53.         $permalink = user_trailingslashit( $permalink, 'single' );
  54.         $permalink = home_url( $permalink );
  55.  
  56.     }
  57.     return apply_filters( 'foxyshop_product_permalink', $permalink, $post->ID );
  58. }
  59.  
  60.  
  61. add_filter('foxyshop_product_url_slug', 'my_foxyshop_product_url_slug', 10, 2);
  62. function my_foxyshop_product_url_slug($slug, $id) {
  63.     if (strpos($slug, '%foxyshop_categories%') !== false) {
  64.         $product_categories = wp_get_post_terms($id, 'foxyshop_categories');
  65.         $category_name = "";
  66.         foreach($product_categories as $term) {
  67.             $category_name = $term->slug;
  68.             if ($term->parent > 0) {
  69.                 $parent_cats = get_ancestors($term->term_id, 'foxyshop_categories');
  70.                 foreach($parent_cats as $parent_id) {
  71.                     $parent_term = get_term_by('id',$parent_id,'foxyshop_categories');
  72.                     $category_name = $parent_term->slug . "/" . $category_name;
  73.                 }
  74.             }
  75.             continue;
  76.         }
  77.         if (!$category_name) $category_name = "base";
  78.         $slug = str_replace('%foxyshop_categories%', $category_name, $slug);
  79.     }
  80.     return $slug;
  81. }
  82.  
  83. add_filter('foxyshop_template_redirect_product_slug', 'my_foxyshop_template_redirect_product_slug');
  84. function my_foxyshop_template_redirect_product_slug($str) {
  85.     return str_replace('/%foxyshop_categories%', '', $str);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement