The really easy way: 更改permalink结构,使其包含“前部”部分。例如:。/news/%postname%
The slightly more complicated and inflexible way: WordPress将其数据永久保存在全局WP_Rewrite
对象存储在$wp_rewrite
. 你可以早一点(大约)来init
) 然后改变这个。
<?php
add_action(\'init\', \'wpse39274_change_date\');
function wpse39274_change_date()
{
global $wp_rewrite;
$wp_rewrite->date_structure = \'/news/%year%/%monthnum%/%day%\';
}
不灵活,因为您已经对数据库进行了硬编码(
news
). 这很好,如果您不需要灵活性,并且希望快速修复,那么您应该这样做。只需确保刷新重写规则:访问“设置”>“永久链接”,然后单击“保存”。
The totally awesome, but a little bit complex way: 在permalinks选项页面中添加一个字段,您可以在其中指定日期基础。
一个用来总结东西的类。
<?php
class Custom_Date_Base
{
const SETTING = \'custom_date_base\';
private static $ins = null;
public static function init()
{
add_action(\'plugins_loaded\', array(self::instance(), \'_setup\'));
}
public static function instance()
{
is_null(self::$ins) && self::$ins = new self;
return self::$ins;
}
public function _setup()
{
// we\'ll hook stuff in here later
}
}
现在我们需要
admin_init
并使用设置API将字段添加到“可选”部分。我们只需要使用
add_settings_field
因为Permalink选项实际上不使用设置API。
<?php
class Custom_Date_Base
{
const SETTING = \'custom_date_base\';
private static $ins = null;
public static function init()
{
add_action(\'plugins_loaded\', array(self::instance(), \'_setup\'));
}
public static function instance()
{
is_null(self::$ins) && self::$ins = new self;
return self::$ins;
}
public function _setup()
{
add_action(\'admin_init\', array($this, \'settings\'));
}
public function settings()
{
add_settings_field(
\'custom-date-base\',
__(\'Date Base\', \'custom-date-base\'),
array($this, \'field_cb\'),
\'permalink\',
\'optional\',
array(\'label_for\' => self::SETTING)
);
}
public function field_cb()
{
printf(
\'<input type="text" class="regular-text" id="%1$s" name="%1$s" value="%2$s" />\',
esc_attr(self::SETTING),
esc_attr(get_option(self::SETTING))
);
}
}
我们还需要
load-options-permalink.php
保存内容(由于缺少设置API支持)。
<?php
class Custom_Date_Base
{
// snip snip
public function _setup()
{
add_action(\'admin_init\', array($this, \'settings\'));
add_action(\'load-options-permalink.php\', array($this, \'save\'));
add_action(\'init\', array($this, \'set_date_base\'));
}
// snip snip
// apparently options-permalink only halfways uses the settings api?
public function save()
{
// make sure it\'s actually an update request.
if(\'POST\' != $_SERVER[\'REQUEST_METHOD\'])
return;
// since this fires before the actual update stuff,
// validate the permalink nonce.
check_admin_referer(\'update-permalink\');
if(!empty($_POST[self::SETTING]))
{
update_option(
self::SETTING,
sanitize_title_with_dashes($_POST[self::SETTING])
);
}
else
{
// remove it.
delete_option(self::SETTING);
}
}
}
最终与
init
并更改数据结构。我们可以借助WordPress通过使用
WP_Rewrite::get_date_permastruct
. 那么这只是一个regex替换的问题。
<?php
class Custom_Date_Base
{
// snip snip
public function _setup()
{
add_action(\'admin_init\', array($this, \'settings\'));
add_action(\'load-options-permalink.php\', array($this, \'save\'));
add_action(\'init\', array($this, \'set_date_base\'));
}
// snip snip
public function set_date_base()
{
if($db = get_option(self::SETTING))
{
global $wp_rewrite;
// current date permastruct
$date_s = $wp_rewrite->get_date_permastruct();
// get the "front" -- stuff before rewrite tags in post links
$front = isset($wp_rewrite->front) ? $wp_rewrite->front : \'/\';
// build some regex. We need to account for the global rewrite
// "front" as well as a possible "/date" that WP appends for
// %post_id% permalink structure where the numbers of a Post ID
// might conflict with with year/month/day numbers.
$regex = \'#^\' . preg_quote($front, \'#\') . \'(/date)?#ui\';
$wp_rewrite->date_structure = preg_replace($regex, "/{$db}/", $date_s);
// apprently we need to make sure this happens?
flush_rewrite_rules();
}
}
}
下面是第三个选项
in a plugin.