1

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' )
        )
    );
2
  • Please describe in detail. Dec 19, 2017 at 12:30
  • Included code snippets.
    – D. Dan
    Dec 19, 2017 at 12:50

1 Answer 1

2

So what I did was:

First make a function that would be returning these slugs based on custom post type name, and if the provided custom slug didn't meet the requirements it falls back to a default value:

function my_get_custom_slugs($cpt){
    $all_the_slugs = [
        "ugly_machine_name_1" => "default-pretty-name-1",
        "ugly_machine_name_2" => "default-pretty-name-2",
        //...etc
    ];
    $r_val = array();
    $options = get_option('my_option_name');
    foreach ($all_the_slugs as $key => $val) {
        if (strlen(trim($options[$key . "_slug"])) > 2){
            $r_val[$key] = $options[$key . "_slug"];
        } else {
            $r_val[$key] = $val;
        }
    }
    return $r_val[$cpt];
}

Of course I now have to rename the id's of the custom options fields to match the custom-post-type-name + "_slug".

And now I can use it in my register post type declarations:

register_post_type( 'this_cpt',
        array(
            'labels' => array(
                'name' => __( 'This CPT Name', 'my-child-theme' ),
                'singular_name' => __( 'This CPT Name', 'my-child-theme' )
            ),
            'public' => true,
            'has_archive' => my_get_custom_slugs('this_cpt'),
            'rewrite' => array( 'slug' => my_get_custom_slugs('this_cpt'), 'with_front' => false ),
            'supports' => array( 'title', 'editor', 'custom-fields' )
        )
    );

And also anywhere in my template files, if I need to.

1
  • haven't experienced any problems with it so far
    – D. Dan
    Dec 27, 2017 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.