使用自定义用户规则创建自定义帖子

时间:2015-12-02 作者:Shiv Singh

我正在创建一个自定义用户帖子类型的推荐,并想给用户权限编辑和删除那里的帖子,但它显示一些错误。

1. I created user rule.

    function add_testimonial_management_role() {
     add_role(\'testimonial_manager\',
                \'Testimonial Manager\',
                array(
                    \'read\' => true,
                    \'edit_posts\' => false,
                    \'delete_posts\' => false,
                    \'publish_posts\' => false,
                    \'upload_files\' => true,
                )
            );
       }
register_activation_hook( __FILE__, \'add_testimonial_management_role\' );

2. Next added custom post

add_action( \'init\', \'register_cpt_testimonials\');
function register_cpt_testimonials() {
            $args = array(
            \'label\'               => __( \'testimonials\', \'testimonials\' ),
            \'description\'         => __( \'Testimonial\', \'testimonials\' ),
            \'labels\'              => $labels,
            \'supports\'            => array( \'title\', \'comments\', \'revisions\', ),
            \'hierarchical\'        => false,
            \'public\'              => true,
            \'show_ui\'             => true,
            \'rewrite\'             => $rewrite,
                        \'capability_type\'     => array(\'testimonial\',\'testimonials\'),
                        \'map_meta_cap\'        => true,
        );
        register_post_type( \'testimonials\', $args );
}

3. last i created rules

add_action(\'admin_init\',\'add_role_caps\',999);
    function add_role_caps() {

        // Add the roles you\'d like to administer the custom post types
        $roles = array(\'testimonial_manager\',\'editor\',\'administrator\');

        // Loop through each role and assign capabilities
        foreach($roles as $the_role) { 

             $role = get_role($the_role);

                 $role->add_cap( \'read\' );
                 $role->add_cap( \'read_testimonial\');
                 $role->add_cap( \'read_private_testimonials\' );
                 $role->add_cap( \'edit_testimonial\' );
                 $role->add_cap( \'edit_testimonials\' );
                 $role->add_cap( \'edit_others_testimonials\' );
                 $role->add_cap( \'edit_published_testimonials\' );
                 $role->add_cap( \'publish_testimonials\' );
                 $role->add_cap( \'delete_others_testimonials\' );
                 $role->add_cap( \'delete_private_testimonials\' );
                 $role->add_cap( \'delete_published_testimonials\' );

        }}
但这表明你最后有这样的错误

Notice: Undefined variable: labels in /home/mortgag1/public_html/wp-content/themes/mortgage/functions.php on line 21

Notice: Undefined variable: rewrite in /home/mortgag1/public_html/wp-content/themes/mortgage/functions.php on line 26

Fatal error: Call to a member function add_cap() on a non-object in /home/mortgag1/public_html/wp-content/themes/mortgage/functions.php on line 43
请帮助我任何人。谢谢

1 个回复
最合适的回答,由SO网友:Shiv Singh 整理而成

我通过替换一些代码来解决我的问题,如下所示

我替换了

   register_activation_hook( __FILE__, \'psp_add_project_management_role\' );

add_action(\'admin_init\',\'add_testimonial_management_role\',998);