我不会试图定位你的鼻涕虫。相反,为什么不给用户一个选项,通过在永久链接设置页面中添加另一个字段来更改它们呢?
钩入load-options-permalink.php
设置一些东西来捕捉$_POST
用于保存slug的数据。还向页面添加设置字段。
<?php
add_action( \'load-options-permalink.php\', \'wpse30021_load_permalinks\' );
function wpse30021_load_permalinks()
{
if( isset( $_POST[\'wpse30021_cpt_base\'] ) )
{
update_option( \'wpse30021_cpt_base\', sanitize_title_with_dashes( $_POST[\'wpse30021_cpt_base\'] ) );
}
// Add a settings field to the permalink page
add_settings_field( \'wpse30021_cpt_base\', __( \'CPT Base\' ), \'wpse30021_field_callback\', \'permalink\', \'optional\' );
}
然后,设置字段的回调功能:
<?php
function wpse30021_field_callback()
{
$value = get_option( \'wpse30021_cpt_base\' );
echo \'<input type="text" value="\' . esc_attr( $value ) . \'" name="wpse30021_cpt_base" id="wpse30021_cpt_base" class="regular-text" />\';
}
然后,当你注册你的帖子类型时,用
get_option
. 如果不存在,请使用默认值。
<?php
add_action( \'init\', \'wpse30021_register_post_type\' );
function wpse30021_register_post_type()
{
$slug = get_option( \'wpse30021_cpt_base\' );
if( ! $slug ) $slug = \'your-default-slug\';
// register your post type, reference $slug for the rewrite
$args[\'rewrite\'] = array( \'slug\' => $slug );
// Obviously you probably need more $args than one....
register_post_type( \'wpse30021_pt\', $args );
}
以下是设置字段部分作为插件
https://gist.github.com/1275867EDIT: Another Option
您还可以根据
WPLANG
常数
只需编写一个保存数据的快速函数。。。
<?php
function wpse30021_get_slug()
{
// return a default slug
if( ! defined( \'WPLANG\' ) || ! WPLANG || \'en_US\' == WPLANG ) return \'press\';
// array of slug data
$slugs = array(
\'fr_FR\' => \'presse\',
\'es_ES\' => \'prensa\'
// etc.
);
return $slugs[WPLANG];
}
然后在注册自定义帖子类型的地方获取slug。
<?php
add_action( \'init\', \'wpse30021_register_post_type\' );
function wpse30021_register_post_type()
{
$slug = wpse30021_get_slug();
// register your post type, reference $slug for the rewrite
$args[\'rewrite\'] = array( \'slug\' => $slug );
// Obviously you probably need more $args than one....
register_post_type( \'wpse30021_pt\', $args );
}
IMO的最佳选择是既给用户一个选项,又提供可靠的默认值:
<?php
add_action( \'init\', \'wpse30021_register_post_type\' );
function wpse30021_register_post_type()
{
$slug = get_option( \'wpse30021_cpt_base\' );
// They didn\'t set up an option, get the default
if( ! $slug ) $slug = wpse30021_get_slug();
// register your post type, reference $slug for the rewrite
$args[\'rewrite\'] = array( \'slug\' => $slug );
// Obviously you probably need more $args than one....
register_post_type( \'wpse30021_pt\', $args );
}