在wp admin中重写自定义POST类型slug

时间:2017-12-19 作者:D. Dan

我正在开发一个网站,最终将有一个副本,一个单独的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\' )
        )
    );

1 个回复
最合适的回答,由SO网友:D. Dan 整理而成

所以我所做的是:

首先,创建一个函数,该函数将根据自定义帖子类型名称返回这些slug,如果提供的自定义slug不符合要求,则返回默认值:

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];
}
当然,我现在必须重命名自定义选项字段的id,以匹配自定义帖子类型名称+“\\u slug”。

现在我可以在register post类型声明中使用它:

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\' )
        )
    );
以及模板文件中的任何位置,如果需要的话。

结束

相关推荐