如何允许自定义用户角色仅创建、编辑、删除特定的自定义帖子类型?

时间:2014-11-18 作者:Riffaz Starr

我创建了一个名为Traveller Post 使用以下代码段:

/* custom post - traveller posts */ 
add_action( \'init\', \'traveller\' );
function traveller() {
  register_post_type( \'traveller\',
    array(
      \'labels\' => array(
        \'name\' => __( \'Traveller Posts\' ),
        \'singular_name\' => __( \'Traveller Post\' )
      ),
      \'public\' => true,
      \'has_archive\' => true,      
    )
  );
}
然后我创建了一个名为Basic Traveller 使用以下代码。

$result = add_role(
    \'basic_traveller\',
    __( \'Basic Traveller\' ),
    array(
        \'read\'         => true,  // true allows this capability
        \'edit_posts\'   => true,
        \'delete_posts\' => false, // Use false to explicitly deny
    )
);
现在用户可以读取和编辑default poststravellers posts 如果他在basic traveler 角色

但我想允许basic traveller 要创建和编辑的角色用户travellers posts 只有我该怎么做?

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

但我想允许基本旅行者角色用户仅创建和编辑旅行者帖子。我该怎么做?

问题是,您有两种不同的帖子类型(帖子和旅行者),许多角色(默认角色加上基本的旅行者),但只有一组功能(默认功能)。

您需要做的是定义一组新的功能,这些功能将映射到您的新角色。这里有一个基本插件,它将添加一个新角色,定义新功能,将这些功能映射到新角色,并将所有新功能分配给管理员。

stackexchange示例。php:

这是我所有插件的主插件文件。它所做的就是为激活、停用和加载的插件添加挂钩。

<?php
/**
 * Plugin Name: Stackexchange Sample
 * Author: Nathan Johnson
 * Licence: GPL2+
 * Licence URI: https://www.gnu.org/licenses/gpl-2.0.en.html
 * Domain Path: /languages
 * Text Domain: stackexchange-sample
 */

//* Don\'t access this file directly
defined( \'ABSPATH\' ) or die();

//* Start bootstraping the plugin
require( dirname( __FILE__ ) . \'/bootstrap.php\' );
add_action( \'plugins_loaded\',
  [ $bootstrap = new \\Stackexchange\\bootstrap(), \'register\' ] );

//* Register activation and deactivation hooks
register_activation_hook( __FILE__ , [ $bootstrap, \'activation\' ] );
register_deactivation_hook( __FILE__ , [ $bootstrap, \'deactivation\' ] );
引导。php

在引导文件中,我们有激活和停用的方法。在激活方法中,我们添加了一个新角色,为其分配了一些功能,然后确保管理员可以使用这些新功能执行所有操作。停用方法与此相反。

唯一的其他方法是寄存器。这里我们添加了一个钩子来注册我们的自定义帖子类型。

<?php
namespace Stackexchange;

class bootstrap {

  public function register() {
    require( dirname( __FILE__ ) . \'/custom-post-type.php\' );
    add_action( \'init\', [ new custom_post_type(), \'register\' ] );
  }

  public function activation() {
    require( dirname( __FILE__ ) . \'/capabilities.php\' );

    $caps = new capabilities( \'traveller\' );
    $caps->add( [
      \'edit_\',
      \'read_\',
      \'delete_\',
      \'edit_s\',
      \'publish_s\',
      \'edit_published_s\',
    ] );

    add_role( \'basic_traveller\', __( \'Basic Traveller\' ), $caps->caps() );

    $admin = get_role( \'administrator\' );
    foreach( $caps->caps() as $cap => $val )
      $admin->add_cap( $cap );
  }

  public function deactivation() {
    remove_role( \'basic_traveller\' );

    $admin = get_role( \'administrator\' );
    $caps = new capabilities( \'traveller\' );
    foreach( $caps->caps() as $cap => $val ) {
      $admin->remove_cap( $cap );
    }
  }
}
能力。php

capabilities类只允许用户轻松定义与新帖子类型交互所需的所有功能。

<?php
namespace Stackexchange;

class capabilities {

  protected $capabilities = [
    //* Meta capabilities
    \'edit_\'              => false,
    \'read_\'              => false,
    \'delete_\'            => false,

    //* Primitive capabilities used outside of map_meta_cap()
    \'edit_s\'             => false,
    \'edit_others_s\'      => false,
    \'publish_s\'          => false,
    \'read_private_s\'     => false,

    //* Primitive capabilities used within of map_meta_cap()
    \'delete_s\'           => false,
    \'delete_private_s\'   => false,
    \'delete_published_s\' => false,
    \'delete_others_s\'    => false,
    \'edit_private_s\'     => false,
    \'edit_published_s\'   => false,
  ];

  public function __construct( $name ) {
    $this->name = $name;
    foreach( $this->capabilities as $key => $val ) {
      $capabilities[ $this->strl_replace( "_", "_{$this->name}", $key ) ] = $val;
    }
    $this->capabilities = $capabilities;
  }

  public function add( $caps = [] ) {
    foreach( $caps as $key ) {
      $this->capabilities[ $this->strl_replace( "_", "_{$this->name}", $key ) ] = true;
    }
  }

  public function caps() {
    return $this->capabilities;
  }

  protected function strl_replace( $search, $replace, $subject ) {
    $pos = strrpos( $subject, $search );
    if( false !== $pos ) {
      $key = substr_replace( $subject, $replace, $pos, strlen( $search ) );
    }
    return $key;
  }
}
自定义帖子类型。php

最后,我们注册自定义的post类型。注意map_meta_cap 是真的。

<?php
namespace Stackexchange;

class custom_post_type {
  public function register() {
    $labels = [
      \'name\'          => __( \'Traveller Posts\' ),
      \'singular_name\' => __( \'Traveller_Post\' ),
    ];
    $args = [
      \'labels\'          => $labels,
      \'public\'          => true,
      \'has_archive\'     => true,
      \'capability_type\' => \'traveller\',
      \'map_meta_cap\'    => true,
    ];
    register_post_type( \'travellers\', $args );
  }
}
basic\\u traveller角色的用户将能够发布和编辑Traveler帖子,但不能对任何其他类型的帖子执行任何操作。

SO网友:Parth Kumar
结束

相关推荐