自定义功能未反映在管理面板上

时间:2015-03-20 作者:Pablo

我制作了一些自定义帖子类型和一些用户角色。每个角色都可以访问不同的自定义帖子类型。

问题是edit\\u others\\u customposttypecapability不起作用。我尝试使用管理员帐户登录,但它无法编辑其他人发布的自定义帖子类型。

我已经安装了用户角色编辑器插件,检查了每个用户的功能,管理员应该能够编辑所有站点的帖子。

以下是用户角色。。。

<?php 

/*

    This page loads all user roles that are necesary to the correct administration of the site.

*/

add_action(\'init\', \'create_metaroles\');

    function create_metaroles() {
        // Defino los roles
        $roles = array();
        $roles[\'Webmaster\'] = array(
            \'role\'              =>  \'webmaster\',
            \'caps\'              =>  array(          
                array(\'pb_institucional\',\'pb_institucionales\'),
                array(\'pb_experto\',\'pb_expertos\'),
                array(\'pb_comite\',\'pb_comites\'),
                array(\'pb_reunion\',\'pb_reuniones\'),
                array(\'pb_progproy\',\'pb_progproys\'),
                array(\'pb_vacante\',\'pb_vacantes\'),
                array(\'pb_pais\',\'pb_paises\'),
                array(\'pb_organizacion\',\'pb_organizaciones\'),
                array(\'pb_evento\',\'pb_eventos\'),
                array(\'pb_noticia\',\'pb_noticias\'),
                array(\'pb_publicacion\',\'pb_publicaciones\'),
                array(\'pb_sector\',\'pb_sectores\'),
                array(\'pb_producto\',\'pb_productos\'),
                array(\'pb_conocimiento\',\'pb_conocimientos\')
            ),
            \'capabilities\'      =>  array(
                \'read\'          =>  true,
                \'edit_posts\'    =>  true,
                \'delete_posts\'  =>  true,
                \'publish_posts\' =>  true,
                \'upload_files\'  =>  true
            )
        );

        $roles[\'Gestor Institucional\'] = array(
            \'role\'              =>  \'gestor_institucional\',
            \'caps\'              =>  array(          
                array(\'pb_institucional\',\'pb_institucionales\'),
                array(\'pb_experto\',\'pb_expertos\'),
                array(\'pb_comite\',\'pb_comites\'),
                array(\'pb_reunion\',\'pb_reuniones\')
            ),
            \'capabilities\'      =>  array(
                \'read\'          =>  true,
                \'edit_posts\'    =>  true,
                \'delete_posts\'  =>  true,
                \'publish_posts\' =>  true,
                \'upload_files\'  =>  true
            )
        );

        $roles[\'Gestor de Programas y Proyectos\'] = array(
            \'role\'              =>  \'gestor_progproy\',
            \'caps\'              =>  array(
            array(\'pb_progproy\',\'pb_progproys\')
            ),
            \'capabilities\'      =>  array(
                \'read\'          =>  true,
                \'edit_posts\'    =>  true,
                \'delete_posts\'  =>  true,
                \'publish_posts\' =>  true,
                \'upload_files\'  =>  true
            )
        );

        $roles[\'Gestor de RRHH\'] = array(
            \'role\'              =>  \'gestor_rrhh\',
            \'caps\'              =>  array(
                array(\'pb_vacante\',\'pb_vacantes\')
            ),
            \'capabilities\'      =>  array(
                \'read\'          =>  true,
                \'edit_posts\'    =>  true,
                \'delete_posts\'  =>  true,
                \'publish_posts\' =>  true,
                \'upload_files\'  =>  true
            )
        );

        $roles[\'Gestor de Comunicacion Institucional\'] = array(
            \'role\'              =>  \'gestor_comunicacion\',
            \'caps\'              =>  array(
                array(\'pb_pais\',\'pb_paises\'),
                array(\'pb_organizacion\',\'pb_organizaciones\'),
                array(\'pb_evento\',\'pb_eventos\'),
                array(\'pb_noticia\',\'pb_noticias\'),
                array(\'pb_publicacion\',\'pb_publicaciones\')
            ),
            \'capabilities\'      =>  array(
                \'read\'          =>  true,
                \'edit_posts\'    =>  true,
                \'delete_posts\'  =>  true,
                \'publish_posts\' =>  true,
                \'upload_files\'  =>  true
            )
        );

        $roles[\'Gestor de Sectores\'] = array(
            \'role\'              =>  \'gestor_sectores\',
            \'caps\'              =>  array(
                array(\'pb_sector\',\'pb_sectores\')
            ),
            \'capabilities\'      =>  array(
                \'read\'          =>  true,
                \'edit_posts\'    =>  true,
                \'delete_posts\'  =>  true,
                \'publish_posts\' =>  true,
                \'upload_files\'  =>  true
            )
        );

        $roles[\'Gestor de Sectores, Programas y Proyectos\'] = array(
            \'role\'              =>  \'gestor_sectores\',
            \'caps\'              =>  array(
                array(\'pb_sector\',\'pb_sectores\'),
                array(\'pb_sector\',\'pb_sectores\')
            ),
            \'capabilities\'      =>  array(
                \'read\'          =>  true,
                \'edit_posts\'    =>  true,
                \'delete_posts\'  =>  true,
                \'publish_posts\' =>  true,
                \'upload_files\'  =>  true
            )
        );

        $roles[\'Gestor de Productos\'] = array(
            \'role\'              =>  \'gestor_productos\',
            \'caps\'              =>  array(
                array(\'pb_producto\',\'pb_productos\')
            ),
            \'capabilities\'      =>  array(
                \'read\'          =>  true,
                \'edit_posts\'    =>  true,
                \'delete_posts\'  =>  true,
                \'publish_posts\' =>  true,
                \'upload_files\'  =>  true
            )
        );

        $roles[\'Gestor de Biblioteca/Conocimiento\'] = array(
            \'role\'              =>  \'gestor_conocimiento\',
            \'caps\'              =>  array(
                array(\'pb_conocimiento\',\'pb_conocimientos\')
            ),
            \'capabilities\'      =>  array(
                \'read\'          =>  true,
                \'edit_posts\'    =>  true,
                \'delete_posts\'  =>  true,
                \'publish_posts\' =>  true,
                \'upload_files\'  =>  true
            )
        );


        foreach( $roles as $name => $arr ){
            add_role($arr[\'role\'], $name,
            array(
                \'read\'          =>  true,
                \'edit_posts\'    =>  false,
                \'delete_posts\'  =>  false,
                \'publish_posts\' =>  false,
                \'upload_files\'  =>  true
            ));

            $rol = get_role( $arr[\'role\'] );

            foreach( $arr[\'caps\'] as $cap ){
                $rol->add_cap( \'read\' );
                $rol->add_cap( \'read_\' . $cap[0] );
                $rol->add_cap( \'read_private_\' . $cap[1] );
                $rol->add_cap( \'edit_\' . $cap[1] );
                $rol->add_cap( \'edit_others_\' . $cap[1] );
                $rol->add_cap( \'edit_published_\' . $cap[1] );
                $rol->add_cap( \'publish_\' . $cap[1] );
                $rol->add_cap( \'delete_\' . $cap[0] );
                $rol->add_cap( \'delete_others_\' . $cap[1] );
                $rol->add_cap( \'delete_private_\' . $cap[1] );
                $rol->add_cap( \'delete_published_\' . $cap[1] );
            }

            $rol = get_role(\'webmaster\');

            $rol->add_cap( \'add_users\' );
            $rol->add_cap( \'create_users\' );
            $rol->add_cap( \'delete_users\' );
            $rol->add_cap( \'edit_users\' );
            $rol->add_cap( \'list_users\' );
            $rol->add_cap( \'promote_users\' );
            $rol->add_cap( \'remove_users\' );
            $rol->add_cap( \'manage_categories\' );
            $rol->add_cap( \'edit_plugins\' );
            $rol->add_cap( \'edit_theme_options\' );
            $rol->add_cap( \'manage_links\' );
            $rol->add_cap( \'manage_options\' );

            $rol = get_role( \'administrator\' );

            foreach( $arr[\'caps\'] as $cap ){
                $rol->add_cap( \'read\' );
                $rol->add_cap( \'read_\' . $cap[0] );
                $rol->add_cap( \'read_private_\' . $cap[1] );
                $rol->add_cap( \'edit_\' . $cap[1] );
                $rol->add_cap( \'edit_others_\' . $cap[1] );
                $rol->add_cap( \'edit_others_\' . $cap[0] );
                $rol->add_cap( \'edit_published_\' . $cap[1] );
                $rol->add_cap( \'publish_\' . $cap[1] );
                $rol->add_cap( \'delete_\' . $cap[0] );
                $rol->add_cap( \'delete_others_\' . $cap[1] );
                $rol->add_cap( \'delete_private_\' . $cap[1] );
                $rol->add_cap( \'delete_published_\' . $cap[1] );
            }
        }
}
?>
我只复制一个自定义的post类型声明(我使用15个,但复制所有声明没有意义)

class PB_Noticias {
        public function __construct(){
            $this->register_post_type();
        }

        public function register_post_type(){
            $labels = array(
                \'name\'                => __( \'Noticias\', \'text-domain\' ),
                \'singular_name\'       => __( \'Noticia\', \'text-domain\' ),
                \'add_new\'             => _x( \'Agregar Noticia\', \'text-domain\', \'text-domain\' ),
                \'add_new_item\'        => __( \'Agregar Noticia\', \'text-domain\' ),
                \'edit_item\'           => __( \'Editar Noticia\', \'text-domain\' ),
                \'new_item\'            => __( \'Nuevo Noticia\', \'text-domain\' ),
                \'view_item\'           => __( \'Ver Noticia\', \'text-domain\' ),
                \'search_items\'        => __( \'Buscar Noticias\', \'text-domain\' ),
                \'not_found\'           => __( \'No se encontraron Noticias\', \'text-domain\' ),
                \'not_found_in_trash\'  => __( \'No se encontraron Noticias en la papelera\', \'text-domain\' ),
                \'parent_item_colon\'   => __( \'Noticia Padre:\', \'text-domain\' ),
                \'menu_name\'           => __( \'Noticias\', \'text-domain\' ),
            );

            $args = array(
                \'labels\'                   => $labels,
                \'hierarchical\'        => false,
                \'description\'         => \'Noticias OLADE\',
                \'taxonomies\'          => array(\'category\', \'post_tag\'),
                \'public\'              => true,
                \'show_ui\'             => true,
                \'show_in_menu\'        => true,
                \'show_in_admin_bar\'   => true,
                \'menu_position\'       => 30,
                \'menu_icon\'           => admin_url() . \'images/project.png\',
                \'show_in_nav_menus\'   => true,
                \'publicly_queryable\'  => true,
                \'exclude_from_search\' => false,
                \'has_archive\'         => true,
                \'query_var\'           => \'noticias\',
                \'can_export\'          => true,
                \'rewrite\'             => array(
                    \'slug\' => \'noticias\'
                    ),
                \'capability_type\'     => array(\'pb_noticia\',\'pb_noticias\'),
                \'supports\'            => array(
                    \'title\', \'editor\'
                    )
            );
            register_post_type(\'pb_noticia\', $args );
        }

        // Galeria
        // Foto grande
        // Cuerpo de la nota
    }
知道为什么edit\\u others\\u cpt不起作用吗?

提前感谢!

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

找到了!我缺少一个自定义功能。在上面的代码中,我添加了这一行:

$rol->add_cap( \'edit_\' . $cap[0] );
它提供了edit\\u ctpsinglename功能。(编辑\\u pb\\u publicacion)。现在它工作得很好。希望有人发现这有用。

SO网友:Laurence Stanley

您的问题是否发生在多站点设置中?如果是这样,下面是一个解决方案:

http://thereforei.am/2011/03/15/how-to-allow-administrators-to-edit-users-in-a-wordpress-network/

默认情况下,在多站点设置中,只有超级管理员可以编辑用户。

结束

相关推荐

Capabilities Vs User Meta

使用capabilities 而不是usermeta? 例如capaccess_feature_x vs元access_feature_x. 我可以使用current_user_can() vs公司get_user_meta() 在各种检查中。那么,功能提供了哪些改进?编辑这个问题似乎过于宽泛,无法给出具体的答案。因此,我将提供一个示例上下文。请保留您的原始答案,因为它们也很有用,并在您当前的答案下面添加其他信息。For example, 我的会员网站上的功能feature_x.</该功能主要对除管