Created
July 15, 2015 08:28
-
-
Save kurozumi/4552c022f29db83465f8 to your computer and use it in GitHub Desktop.
【WordPress】カテゴリーのパーマリンクをカテゴリーIDに変更するプラグイン
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 | |
/* | |
Plugin Name: Numeric Category Link | |
Version: 0.1-alpha | |
Description: PLUGIN DESCRIPTION HERE | |
Author: kurozumi | |
Author URI: http://a-zumi.net | |
Plugin URI: http://a-zumi.net | |
Text Domain: numeric_category_link | |
Domain Path: /languages | |
*/ | |
$numeric_category_link = new Numeric_Category_Link(); | |
$numeric_category_link->register(); | |
class Numeric_Category_Link { | |
public function register() | |
{ | |
add_action('plugins_loaded', array($this, 'plugins_loaded')); | |
} | |
public function plugins_loaded() | |
{ | |
add_filter('category_rewrite_rules', array($this, 'rewrite_rules')); | |
add_filter('category_link', array($this, 'category_link'), 10, 2); | |
} | |
public function rewrite_rules($rules) | |
{ | |
$custom_rules = array(); | |
foreach ($rules as $regex => $rewrite) { | |
$regex = str_replace('/(.+?)/', '/([0-9]{1,})/', $regex); | |
$rewrite = str_replace('?category_name=', '?cat=', $rewrite); | |
$custom_rules[$regex] = $rewrite; | |
} | |
return $custom_rules; | |
} | |
public function category_link($category_link, $term_id) | |
{ | |
global $wp_rewrite; | |
if ($wp_rewrite->using_permalinks()) { | |
$permastruct = $wp_rewrite->get_extra_permastruct('category'); | |
$permastruct = str_replace('%category%', $term_id, $permastruct); | |
$category_link = home_url(user_trailingslashit($permastruct, 'category')); | |
} | |
return $category_link; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment