From 8675a5d87cf6bc7dada2ff84c158d14abae57a02 Mon Sep 17 00:00:00 2001 From: samiahmedsiddiqui Date: Sun, 16 Jul 2017 18:35:00 +0500 Subject: [PATCH] Add product_cat tag which is specially used for WooCommerce Products --- permalinks-customizer.php | 36 ++++++++++++++++++++++++++++++---- readme.txt | 41 ++++++++++++++++++++++----------------- 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/permalinks-customizer.php b/permalinks-customizer.php index 8b89f59..398fc55 100644 --- a/permalinks-customizer.php +++ b/permalinks-customizer.php @@ -2,7 +2,7 @@ /** * Plugin Name: Permalinks Customizer - * Version: 0.3 + * Version: 0.3.1 * Plugin URI: https://wordpress.org/plugins/permalinks-customizer/ * Description: Set permalinks for default post-type and custom post-type which can be changed from the single post edit page. * Author: Sami Ahmed Siddiqui @@ -192,6 +192,10 @@ function permalinks_customizer_tags_page(){ %category% A sanitized version of the category name (category slug field on New/Edit Category panel). Nested sub-categories appear as nested directories in the URI. '; + $html .= ' + %product_cat% + A sanitized version of the product category name (category slug field on New/Edit Category panel). Nested sub-categories appear as nested directories in the URI. This tag is specially used for WooCommerce Products. + '; $html .= ' %author% A sanitized version of the author name. @@ -581,6 +585,30 @@ function permalinks_customizer_replace_tags($post_id, $post, $replace_tag){ } $replace_tag = str_replace('%category%', $category, $replace_tag); } + if(strpos($replace_tag, "%product_cat%") !== false ){ + $categories = get_the_terms($post_id, 'product_cat'); + $total_cat = count($categories); + $tid = 1; + if($total_cat > 0){ + $tid = ''; + foreach($categories as $cat){ + if($cat->term_id < $tid || empty($tid)){ + $tid = $cat->term_id; + $pid = ''; + if(!empty($cat->parent)){ + $pid = $cat->parent; + } + } + } + } + $term_category = get_term($tid); + $category = $term_category->slug; + if(!empty($pid)){ + $parent_category = get_term($pid); + $category = $parent_category->slug.'/'.$category; + } + $replace_tag = str_replace('%product_cat%', $category, $replace_tag); + } if(strpos($replace_tag, "%author%") !== false ){ $author = get_the_author_meta( 'user_login', $post->post_author ); $replace_tag = str_replace('%author%', $author, $replace_tag); @@ -628,7 +656,7 @@ function permalinks_customizer_original_page_link($post_id) { add_action( 'edit_tag_form', 'permalinks_customizer_term_options' ); add_action( 'add_tag_form', 'permalinks_customizer_term_options' ); add_action( 'edit_category_form', 'permalinks_customizer_term_options' ); - add_action('save_post', 'permalinks_customizer_customization', 10, 3); + add_action( 'save_post', 'permalinks_customizer_customization', 10, 3); add_action( 'edited_post_tag', 'permalinks_customizer_save_tag' ); add_action( 'edited_category', 'permalinks_customizer_save_category' ); add_action( 'create_post_tag', 'permalinks_customizer_save_tag' ); @@ -639,5 +667,5 @@ function permalinks_customizer_original_page_link($post_id) { add_action( 'admin_menu', 'permalinks_customizer_menu' ); $plugin = plugin_basename(__FILE__); - add_filter("plugin_action_links_$plugin", 'permalinks_customizer_settings_link' ); -} \ No newline at end of file + add_filter( "plugin_action_links_$plugin", 'permalinks_customizer_settings_link' ); +} diff --git a/readme.txt b/readme.txt index 51e1884..54686aa 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ License URI: http://www.gnu.org/licenses/gpl.html Tags: address, category, custom, custom permalink, custom post permalinks, link, permalink, rewrite slug, redirects, slug, tags, url Requires at least: 3.5 Tested up to: 4.4 -Stable tag: 0.3 +Stable tag: 0.3.1 Set permalinks for default post-type and custom post-type which can be changed from the single post edit page. @@ -21,12 +21,12 @@ By using Permalinks Customizer you can set the permalinks for e Let's assume that you have 6 post-types and they all have different style of permalinks. Like: -1. Blog: For this post type you want to create a permalink which looks like this: http://www.example.com/blog/year-month-date-postname/ -2. Customers: For this post type you want to create a permalink which looks like this: http://www.example.com/customers/postname/ -3. Events: For this post type you want to create a permalink which looks like this: http://www.example.com/events/year-month-date-postname/ -4. Press: For this post type you want to create a permalink which looks like this: http://www.example.com/press/category/year/postname/ -5. News: For this post type you want to create a permalink which looks like this: http://www.example.com/news/year/postname/ -6. Sponsors: For this post type you want to create a permalink which looks like this: http://www.example.com/company/sponsor/post_title/ +1. Blog : For this post type you want to create a permalink which looks like this: http://www.example.com/blog/year-month-date-postname/ +2. Customers : For this post type you want to create a permalink which looks like this: http://www.example.com/customers/postname/ +3. Events : For this post type you want to create a permalink which looks like this: http://www.example.com/events/year-month-date-postname/ +4. Press : For this post type you want to create a permalink which looks like this: http://www.example.com/press/category/year/postname/ +5. News : For this post type you want to create a permalink which looks like this: http://www.example.com/news/year/postname/ +6. Sponsors : For this post type you want to create a permalink which looks like this: http://www.example.com/company/sponsor/post_title/ This plugin allows you to do this very easily. You just need to go on Permalinks Customizer Settings Page. Where text fields are shown with post-type name. You can define your permalinks you want to create for each post type. @@ -38,17 +38,18 @@ If you leave the some post-type fields empty. So, Permalinks Customizer< = Structure Tags = -* %title%: Title of the post. let's say the title is "This Is A Great Post!" so, it becomes this-is-a-great-post in the URI. -* %year%: The year of the post, four digits, for example 2004 -* %monthnum%: Month of the year, for example 05 -* %day%: Day of the month, for example 28 -* %hour%: Hour of the day, for example 15 -* %minute%: Minute of the hour, for example 43 -* %second%: Second of the minute, for example 33 -* %post_id%: The unique ID # of the post, for example 423 -* %postname%: A sanitized version of the title of the post (post slug field on Edit Post/Page panel). So "This Is A Great Post!" becomes this-is-a-great-post in the URI. -* %category%: A sanitized version of the category name (category slug field on New/Edit Category panel). Nested sub-categories appear as nested directories in the URI. -* %author%: A sanitized version of the author name. +* %title% : Title of the post. let's say the title is "This Is A Great Post!" so, it becomes this-is-a-great-post in the URI. +* %year% : The year of the post, four digits, for example 2004 +* %monthnum% : Month of the year, for example 05 +* %day% : Day of the month, for example 28 +* %hour% : Hour of the day, for example 15 +* %minute% : Minute of the hour, for example 43 +* %second% : Second of the minute, for example 33 +* %post_id% : The unique ID # of the post, for example 423 +* %postname% : A sanitized version of the title of the post (post slug field on Edit Post/Page panel). So "This Is A Great Post!" becomes this-is-a-great-post in the URI. +* %category% : A sanitized version of the category name (category slug field on New/Edit Category panel). Nested sub-categories appear as nested directories in the URI. +* %product_cat% : A sanitized version of the product category name (category slug field on New/Edit Category panel). Nested sub-categories appear as nested directories in the URI. This tag is specially used for WooCommerce Products. +* %author% : A sanitized version of the author name. Note: *%postname%* is similar as of the *%title%* tag but the difference is that *%postname%* can only be set once whereas *%title%* can be changed. let's say the title is "This Is A Great Post!" so, it becomes "this-is-a-great-post" in the URI(At the first time, *%postname%* and *%title%* works same) but if you edit and change title let's say "This Is A WordPress Post!" so, *%postname%* in the URI remains same "this-is-a-great-post" whereas *%title%* in the URI becomes "this-is-a-wordpress-post" @@ -85,6 +86,10 @@ A. No, This plugin does not work with [custom permalinks](https://wordpress.org/ == Changelog == += 0.3.1 = + + * Add product_cat tag which is specially used for WooCommerce Products + = 0.3 = * Add functionality to create a permalink for category and tag for default post type