I'm developing a site that will eventually have a copy, a separate WP install in another language. I have a few custom post types in this site, some of them with custom slugs for archive page and/or for singular posts. I was wondering could I use options from a custom admin page to do this? Does this approach have any pitfalls?
So I would define a custom option to be used in 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();
}
Then try using the value of the option as the slug for registering post types:
$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' )
)
);
I am aware that after every change in those options the permalinks option would have to be loaded for the settings to take effect, but it would only be changed once when the site is copied.
I also tried this before for similar purposes on another page, and it didn't worked:
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' )
)
);