当您创建自定义帖子类型时,是否也会创建自动编辑/删除该帖子类型的功能?

时间:2011-06-28 作者:trusktr

例如,如果我创建一个名为“destinations”的帖子类型,它会自动创建“edit\\u destinations”或“delete\\u destinations”之类的功能吗?

2 个回复
最合适的回答,由SO网友:aaronwaggs 整理而成

在没有向WordPress注册新功能的情况下,它不会自动创建该功能。相反,它默认使用分配给创建/编辑帖子的功能。例如,如果作者登录,默认情况下,他们将能够创建和发布新的目标条目。

您可以使用capabilities 使用时的值register_post_type. 请参见Justin Tadlock的优秀教程http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress

SO网友:bueltge

我为自定义帖子类型定义了一个中心变量:public $post_type_1 = \'archiv\';

并将其用于添加新功能:

        $capabilities = array(
            \'edit_post\'          => \'edit_\' . $this->post_type_1,
            \'edit_posts\'         => \'edit_\' . $this->post_type_1 . \'s\',
            \'edit_others_posts\'  => \'edit_others_\' . $this->post_type_1 . \'s\',
            \'publish_posts\'      => \'publish_\' . $this->post_type_1 . \'s\',
            \'read_post\'          => \'read_\' . $this->post_type_1,
            \'read_private_posts\' => \'read_private_\' . $this->post_type_1 . \'s\',
            \'delete_post\'        => \'delete_\' . $this->post_type_1
        );
此外,我仅在激活插件时,才将此新功能对象添加到不同的默认角色:

        foreach ( $this->todo_roles as $role ) {
            $wp_roles->add_cap( $role, \'edit_\'          . $this->post_type_1 );
            $wp_roles->add_cap( $role, \'edit_\'          . $this->post_type_1 . \'s\' );
            $wp_roles->add_cap( $role, \'edit_others_\'   . $this->post_type_1 . \'s\' );
            $wp_roles->add_cap( $role, \'publish_\'       . $this->post_type_1 . \'s\' );
            $wp_roles->add_cap( $role, \'read_\'          . $this->post_type_1 );
            $wp_roles->add_cap( $role, \'read_private_\'  . $this->post_type_1 . \'s\' );
            $wp_roles->add_cap( $role, \'delete_\'        . $this->post_type_1 );
            $wp_roles->add_cap( $role, \'manage_\'        . $this->taxonomy_type_1 );
        }

        foreach ( $this->read_roles as $role ) {
            $wp_roles->add_cap( $role, \'read_\' . $this->post_type_1 );
            $wp_roles->add_cap( $role, \'read_\' . $this->post_type_1 );
            $wp_roles->add_cap( $role, \'read_\' . $this->post_type_1 );
        }

        global $wp_rewrite;
        $wp_rewrite->flush_rules();
但是,如果要卸载插件,还必须注销此对象。

您可以在此要点上看到一个示例:https://gist.github.com/978690

结束

相关推荐