Skip to content

Commit

Permalink
Add product_cat tag which is specially used for WooCommerce Products
Browse files Browse the repository at this point in the history
  • Loading branch information
samiahmedsiddiqui committed Jul 16, 2017
1 parent ce895ab commit 8675a5d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
36 changes: 32 additions & 4 deletions permalinks-customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -192,6 +192,10 @@ function permalinks_customizer_tags_page(){
<th scope="row">%category%</th>
<td>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.</td>
</tr>';
$html .= '<tr valign="top">
<th scope="row">%product_cat%</th>
<td>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. <i>This <strong>tag</strong> is specially used for WooCommerce Products.</i></td>
</tr>';
$html .= '<tr valign="top">
<th scope="row">%author%</th>
<td>A sanitized version of the author name.</td>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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' );
Expand All @@ -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' );
}
add_filter( "plugin_action_links_$plugin", 'permalinks_customizer_settings_link' );
}
41 changes: 23 additions & 18 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -21,12 +21,12 @@ By using <strong>Permalinks Customizer</strong> you can set the permalinks for e

Let's assume that you have 6 <strong>post-types</strong> and they all have different style of <strong>permalinks</strong>. Like:

1. <strong>Blog:</strong> For this post type you want to create a <strong>permalink</strong> which looks like this: http://www.example.com/blog/year-month-date-postname/
2. <strong>Customers:</strong> For this post type you want to create a <strong>permalink</strong> which looks like this: http://www.example.com/customers/postname/
3. <strong>Events:</strong> For this post type you want to create a <strong>permalink</strong> which looks like this: http://www.example.com/events/year-month-date-postname/
4. <strong>Press:</strong> For this post type you want to create a <strong>permalink</strong> which looks like this: http://www.example.com/press/category/year/postname/
5. <strong>News:</strong> For this post type you want to create a <strong>permalink</strong> which looks like this: http://www.example.com/news/year/postname/
6. <strong>Sponsors:</strong> For this post type you want to create a <strong>permalink</strong> which looks like this: http://www.example.com/company/sponsor/post_title/
1. <strong>Blog :</strong> For this post type you want to create a <strong>permalink</strong> which looks like this: http://www.example.com/blog/year-month-date-postname/
2. <strong>Customers :</strong> For this post type you want to create a <strong>permalink</strong> which looks like this: http://www.example.com/customers/postname/
3. <strong>Events :</strong> For this post type you want to create a <strong>permalink</strong> which looks like this: http://www.example.com/events/year-month-date-postname/
4. <strong>Press :</strong> For this post type you want to create a <strong>permalink</strong> which looks like this: http://www.example.com/press/category/year/postname/
5. <strong>News :</strong> For this post type you want to create a <strong>permalink</strong> which looks like this: http://www.example.com/news/year/postname/
6. <strong>Sponsors :</strong> For this post type you want to create a <strong>permalink</strong> 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 <strong>Permalinks Customizer</strong> Settings Page. Where text fields are shown with post-type name. You can define your permalinks you want to create for each post type.

Expand All @@ -38,17 +38,18 @@ If you leave the some post-type fields empty. So, <strong>Permalinks Customizer<

= Structure Tags =

* <strong>%title%</strong>: 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.
* <strong>%year%</strong>: The year of the post, four digits, for example 2004
* <strong>%monthnum%</strong>: Month of the year, for example 05
* <strong>%day%</strong>: Day of the month, for example 28
* <strong>%hour%</strong>: Hour of the day, for example 15
* <strong>%minute%</strong>: Minute of the hour, for example 43
* <strong>%second%</strong>: Second of the minute, for example 33
* <strong>%post_id%</strong>: The unique ID # of the post, for example 423
* <strong>%postname%</strong>: 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.
* <strong>%category%</strong>: 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.
* <strong>%author%</strong>: A sanitized version of the author name.
* <strong>%title%</strong> : 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.
* <strong>%year%</strong> : The year of the post, four digits, for example 2004
* <strong>%monthnum%</strong> : Month of the year, for example 05
* <strong>%day%</strong> : Day of the month, for example 28
* <strong>%hour%</strong> : Hour of the day, for example 15
* <strong>%minute%</strong> : Minute of the hour, for example 43
* <strong>%second%</strong> : Second of the minute, for example 33
* <strong>%post_id%</strong> : The unique ID # of the post, for example 423
* <strong>%postname%</strong> : 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.
* <strong>%category%</strong> : 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.
* <strong>%product_cat%</strong> : 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. <i>This <strong>tag</strong> is specially used for WooCommerce Products.</i>
* <strong>%author%</strong> : A sanitized version of the author name.

<strong>Note</strong>: *%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"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8675a5d

Please sign in to comment.