我正在开发一个网站,最终将有一个副本,一个单独的WP安装在另一种语言。我在这个网站上有一些自定义帖子类型,其中一些带有用于存档页面和/或单发帖子的自定义slug。我想知道我是否可以使用自定义管理页面中的选项来执行此操作?这种方法有什么缺陷吗?
因此,我将定义一个在WP Admin中使用的自定义选项:
class MySettingsPage
{
private $options;
public function __construct()
{
add_action( \'admin_menu\', array( $this, \'add_plugin_page\' ) );
add_action( \'admin_init\', array( $this, \'page_init\' ) );
}
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
\'Settings Admin\',
\'Custom Settings\',
\'manage_options\',
\'my-setting-admin\',
array( $this, \'create_admin_page\' )
);
}
public function create_admin_page()
{
// Set class property
$this->options = get_option( \'my_option_name\' );
?>
<div class="wrap">
<h1>Custom Settings</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( \'my_option_group\' );
do_settings_sections( \'my-setting-admin\' );
submit_button();
?>
</form>
</div>
<?php
}
public function page_init()
{
register_setting(
\'my_option_group\', // Option group
\'my_option_name\', // Option name
array( $this, \'sanitize\' ) // Sanitize
);
add_settings_section(
\'setting_section_id\', // ID
\'My Custom Settings\', // Title
array( $this, \'print_section_info\' ), // Callback
\'my-setting-admin\' // Page
);
add_settings_field(
\'custom_slug\', // slug
\'Custom Slug\', // Title
array( $this, \'custom_slug_callback\' ), // Callback
\'my-setting-admin\', // Page
\'setting_section_id\' // Section
);
}
public function sanitize( $input )
{
$new_input = array();
if( isset( $input[\'custom_slug\'] ) )
$new_input[\'custom_slug\'] = sanitize_text_field( $input[\'custom_slug\'] );
return $new_input;
}
public function print_section_info()
{
print \'Enter your settings below:\';
}
public function custom_slug_callback()
{
printf(
\'<input type="text" id="custom_slug" name="my_option_name[custom_slug]" value="%s" />\',
isset( $this->options[\'custom_slug\'] ) ? esc_attr( $this->options[\'custom_slug\']) : \'\'
);
}
}
if( is_admin() ) {
$my_settings_page = new MySettingsPage();
}
然后尝试使用选项的值作为slug来注册帖子类型:
$o = get_option(\'my_option_name\');
$s = $o[\'custom_slug\'];
register_post_type( \'ugly_machine_name\',
array(
\'labels\' => array(
\'name\' => __( \'Pretty Name\', \'my-child-theme\' ),
\'singular_name\' => __( \'Pretty Name\', \'my-child-theme\' )
),
\'public\' => true,
\'has_archive\' => $s,
\'rewrite\' => array( \'slug\' => $s, \'with_front\' => false ),
\'supports\' => array( \'title\', \'editor\', \'custom-fields\' )
)
);
我知道,每次更改这些选项后,必须加载永久链接选项才能使设置生效,但只有在复制站点时才会更改一次。
我之前也在另一个页面上尝试过类似的方法,但没有成功:
register_post_type( \'ugly_machine_name\',
array(
\'labels\' => array(
\'name\' => __( \'Pretty Name\', \'my-child-theme\' ), // these got
\'singular_name\' => __( \'Pretty Name\', \'my-child-theme\' ) // loaded
),
\'public\' => true,
\'has_archive\' => __( \'pretty-name\', \'my-child-theme\' ), // theese didn\'t load (the translations)
\'rewrite\' => array( \'slug\' => __( \'pretty-name\', \'my-child-theme\' ), \'with_front\' => false ),
\'supports\' => array( \'title\', \'editor\', \'custom-fields\' )
)
);