HOME > WEBプログラム覚書 > WordPressで会員サイト開発:Role追加編

WordPressで会員サイト開発:Role追加編

WordPressでこんな感じのサイトを作る。

内容は適当。要は3つのユーザータイプがいて、それぞれ出来ることが異なるようなサイトを作る。

まぁ作るといっても作れるかどうかは不明。なぜならまだ制作途中だからw なのでコードはテストも兼ねてたりで書き殴り状態なので真似せず自分で書いた方がよいと思いますw

Wordpressの設定

3.0からカスタム投稿タイプって便利なのができたらしいので、 1つのサイトでも、よさそうですがとりあえずマルチサイトONにします。

ブログはディレクトリごとにして、ネットワーク管理者 > 設定 > ユーザーの登録の設定は「ユーザーアカウントの新規登録を許可する。」 に設定しときます。

で、適当にプラグイン作ってネットワークで有効にしときます。 以降のスクリプトは全てそのプラグインに記述します。

新規Roleの追加

とりあえず、3つのタイプを追加する。権限は適当。あとで調整する。

PHP
  1.  <?php
  2.  // capを付けたり外したりして管理画面がどのように変化するか試したい場合、
  3.  // コメントアウトを解除する
  4.  
  5.  //remove_role('corporate');
  6.  //remove_role('client');
  7.  //remove_role('worker');
  8.  
  9.  
  10.  // プラグイン呼ばれた段階ではWP_Rolesが
  11.  // インスタンス化されてないっぽい??
  12.  $wp_roles =& new WP_Roles();
  13.  
  14.  
  15.  function nnco_init()
  16.  {
  17.   nnco_registRole();
  18.  }
  19.  
  20.  /**
  21.   * 現在設定されてるロールにNnco用のロールがない場合追加
  22.   * 存在してたらどうする?
  23.   *
  24.   * 追加するロールは class NncoRolesから取得
  25.   *
  26.   *
  27.   * @return void
  28.   */
  29.  function nnco_registRole()
  30.  {
  31.   global $wp_roles;
  32.   foreach (NncoRoles::find() as $key => $value) {
  33.   if (!array_key_exists($key, $wp_roles->roles)) {
  34.   add_role($key, __($value['name']));
  35.   $role = get_role($key);
  36.   // 権限の設定
  37.   foreach ($value['caps'] as $cap) {
  38.   $role->add_cap($cap);
  39.   }
  40.   }
  41.   }
  42.  }
  43.  
  44.  /**
  45.   * ユーザーのタイプと権限などを突っ込んでおく。
  46.   *
  47.   *
  48.   */
  49.  class NncoRoles
  50.  {
  51.   private static $roles = array(
  52.   'corporate' => array(
  53.   'name' => '企業',
  54.   'caps' => array(
  55.   'read',
  56.   'edit_posts',
  57.   'publish_posts',
  58.   'read_private_posts',
  59.   'edit_published_posts',
  60.   'edit_users'
  61.   ),
  62.   ),
  63.  
  64.   'client' => array(
  65.   'name' => 'クライアント',
  66.   'caps' => array('edit_posts', 'read'),
  67.   ),
  68.  
  69.   'worker' => array(
  70.   'name' => '派遣労働者',
  71.   'caps' => array('edit_posts', 'read'),
  72.   ),
  73.   );
  74.  
  75.   public static function find($key = null)
  76.   {
  77.   if (is_null($key)) {
  78.   return NncoRoles::$roles;
  79.   } else {
  80.   if (isset(NncoRoles::$roles[$key])) {
  81.   return NncoRoles::$roles[$key];
  82.   } else {
  83.   return false;
  84.   }
  85.   }
  86.   }
  87.  
  88.   public static function findByDefaultChecked()
  89.   {
  90.   return 'corporate';
  91.   }
  92.  }
  93.  
  94.  // Main
  95.  nnco_init();
  96.  ?>

実行後、roleの中をのぞいてみる

var_dump($wp_roles->roles)
  1.  <?php
  2.  array(8) {
  3.   ["administrator"]=>
  4.   array(2) {
  5.   ["name"]=>
  6.   string(13) "Administrator"
  7.   ["capabilities"]=>
  8.   array(62) {
  9.   ["switch_themes"]=>
  10.   bool(true)
  11.   ["edit_themes"]=>
  12.   bool(true)
  13.   ["activate_plugins"]=>
  14.   bool(true)
  15.   ["edit_plugins"]=>
  16.   bool(true)
  17.   ["edit_users"]=>
  18.   bool(true)
  19.   ["edit_files"]=>
  20.   bool(true)
  21.   ["manage_options"]=>
  22.   bool(true)
  23.   ["moderate_comments"]=>
  24.   bool(true)
  25.   ["manage_categories"]=>
  26.   bool(true)
  27.   ["manage_links"]=>
  28.   bool(true)
  29.   ["upload_files"]=>
  30.   bool(true)
  31.   ["import"]=>
  32.   bool(true)
  33.   ["unfiltered_html"]=>
  34.   bool(true)
  35.   ["edit_posts"]=>
  36.   bool(true)
  37.   ["edit_others_posts"]=>
  38.   bool(true)
  39.   ["edit_published_posts"]=>
  40.   bool(true)
  41.   ["publish_posts"]=>
  42.   bool(true)
  43.   ["edit_pages"]=>
  44.   bool(true)
  45.   ["read"]=>
  46.   bool(true)
  47.   ["level_10"]=>
  48.   bool(true)
  49.   ["level_9"]=>
  50.   bool(true)
  51.   ["level_8"]=>
  52.   bool(true)
  53.   ["level_7"]=>
  54.   bool(true)
  55.   ["level_6"]=>
  56.   bool(true)
  57.   ["level_5"]=>
  58.   bool(true)
  59.   ["level_4"]=>
  60.   bool(true)
  61.   ["level_3"]=>
  62.   bool(true)
  63.   ["level_2"]=>
  64.   bool(true)
  65.   ["level_1"]=>
  66.   bool(true)
  67.   ["level_0"]=>
  68.   bool(true)
  69.   ["edit_others_pages"]=>
  70.   bool(true)
  71.   ["edit_published_pages"]=>
  72.   bool(true)
  73.   ["publish_pages"]=>
  74.   bool(true)
  75.   ["delete_pages"]=>
  76.   bool(true)
  77.   ["delete_others_pages"]=>
  78.   bool(true)
  79.   ["delete_published_pages"]=>
  80.   bool(true)
  81.   ["delete_posts"]=>
  82.   bool(true)
  83.   ["delete_others_posts"]=>
  84.   bool(true)
  85.   ["delete_published_posts"]=>
  86.   bool(true)
  87.   ["delete_private_posts"]=>
  88.   bool(true)
  89.   ["edit_private_posts"]=>
  90.   bool(true)
  91.   ["read_private_posts"]=>
  92.   bool(true)
  93.   ["delete_private_pages"]=>
  94.   bool(true)
  95.   ["edit_private_pages"]=>
  96.   bool(true)
  97.   ["read_private_pages"]=>
  98.   bool(true)
  99.   ["delete_users"]=>
  100.   bool(true)
  101.   ["create_users"]=>
  102.   bool(true)
  103.   ["unfiltered_upload"]=>
  104.   bool(true)
  105.   ["edit_dashboard"]=>
  106.   bool(true)
  107.   ["update_plugins"]=>
  108.   bool(true)
  109.   ["delete_plugins"]=>
  110.   bool(true)
  111.   ["install_plugins"]=>
  112.   bool(true)
  113.   ["update_themes"]=>
  114.   bool(true)
  115.   ["install_themes"]=>
  116.   bool(true)
  117.   ["update_core"]=>
  118.   bool(true)
  119.   ["list_users"]=>
  120.   bool(true)
  121.   ["remove_users"]=>
  122.   bool(true)
  123.   ["add_users"]=>
  124.   bool(true)
  125.   ["promote_users"]=>
  126.   bool(true)
  127.   ["edit_theme_options"]=>
  128.   bool(true)
  129.   ["delete_themes"]=>
  130.   bool(true)
  131.   ["export"]=>
  132.   bool(true)
  133.   }
  134.   }
  135.   ["editor"]=>
  136.   array(2) {
  137.   ["name"]=>
  138.   string(6) "Editor"
  139.   ["capabilities"]=>
  140.   array(34) {
  141.   ["moderate_comments"]=>
  142.   bool(true)
  143.   ["manage_categories"]=>
  144.   bool(true)
  145.   ["manage_links"]=>
  146.   bool(true)
  147.   ["upload_files"]=>
  148.   bool(true)
  149.   ["unfiltered_html"]=>
  150.   bool(true)
  151.   ["edit_posts"]=>
  152.   bool(true)
  153.   ["edit_others_posts"]=>
  154.   bool(true)
  155.   ["edit_published_posts"]=>
  156.   bool(true)
  157.   ["publish_posts"]=>
  158.   bool(true)
  159.   ["edit_pages"]=>
  160.   bool(true)
  161.   ["read"]=>
  162.   bool(true)
  163.   ["level_7"]=>
  164.   bool(true)
  165.   ["level_6"]=>
  166.   bool(true)
  167.   ["level_5"]=>
  168.   bool(true)
  169.   ["level_4"]=>
  170.   bool(true)
  171.   ["level_3"]=>
  172.   bool(true)
  173.   ["level_2"]=>
  174.   bool(true)
  175.   ["level_1"]=>
  176.   bool(true)
  177.   ["level_0"]=>
  178.   bool(true)
  179.   ["edit_others_pages"]=>
  180.   bool(true)
  181.   ["edit_published_pages"]=>
  182.   bool(true)
  183.   ["publish_pages"]=>
  184.   bool(true)
  185.   ["delete_pages"]=>
  186.   bool(true)
  187.   ["delete_others_pages"]=>
  188.   bool(true)
  189.   ["delete_published_pages"]=>
  190.   bool(true)
  191.   ["delete_posts"]=>
  192.   bool(true)
  193.   ["delete_others_posts"]=>
  194.   bool(true)
  195.   ["delete_published_posts"]=>
  196.   bool(true)
  197.   ["delete_private_posts"]=>
  198.   bool(true)
  199.   ["edit_private_posts"]=>
  200.   bool(true)
  201.   ["read_private_posts"]=>
  202.   bool(true)
  203.   ["delete_private_pages"]=>
  204.   bool(true)
  205.   ["edit_private_pages"]=>
  206.   bool(true)
  207.   ["read_private_pages"]=>
  208.   bool(true)
  209.   }
  210.   }
  211.   ["author"]=>
  212.   array(2) {
  213.   ["name"]=>
  214.   string(6) "Author"
  215.   ["capabilities"]=>
  216.   array(10) {
  217.   ["upload_files"]=>
  218.   bool(true)
  219.   ["edit_posts"]=>
  220.   bool(true)
  221.   ["edit_published_posts"]=>
  222.   bool(true)
  223.   ["publish_posts"]=>
  224.   bool(true)
  225.   ["read"]=>
  226.   bool(true)
  227.   ["level_2"]=>
  228.   bool(true)
  229.   ["level_1"]=>
  230.   bool(true)
  231.   ["level_0"]=>
  232.   bool(true)
  233.   ["delete_posts"]=>
  234.   bool(true)
  235.   ["delete_published_posts"]=>
  236.   bool(true)
  237.   }
  238.   }
  239.   ["contributor"]=>
  240.   array(2) {
  241.   ["name"]=>
  242.   string(11) "Contributor"
  243.   ["capabilities"]=>
  244.   array(5) {
  245.   ["edit_posts"]=>
  246.   bool(true)
  247.   ["read"]=>
  248.   bool(true)
  249.   ["level_1"]=>
  250.   bool(true)
  251.   ["level_0"]=>
  252.   bool(true)
  253.   ["delete_posts"]=>
  254.   bool(true)
  255.   }
  256.   }
  257.   ["subscriber"]=>
  258.   array(2) {
  259.   ["name"]=>
  260.   string(10) "Subscriber"
  261.   ["capabilities"]=>
  262.   array(2) {
  263.   ["read"]=>
  264.   bool(true)
  265.   ["level_0"]=>
  266.   bool(true)
  267.   }
  268.   }
  269.   ["corporate"]=>
  270.   array(2) {
  271.   ["name"]=>
  272.   string(9) "企業"
  273.   ["capabilities"]=>
  274.   array(2) {
  275.   ["edit_posts"]=>
  276.   bool(true)
  277.   ["read"]=>
  278.   bool(true)
  279.   }
  280.   }
  281.   ["client"]=>
  282.   array(2) {
  283.   ["name"]=>
  284.   string(18) "クライアント"
  285.   ["capabilities"]=>
  286.   array(2) {
  287.   ["edit_posts"]=>
  288.   bool(true)
  289.   ["read"]=>
  290.   bool(true)
  291.   }
  292.   }
  293.   ["worker"]=>
  294.   array(2) {
  295.   ["name"]=>
  296.   string(9) "派遣労働者"
  297.   ["capabilities"]=>
  298.   array(2) {
  299.   ["edit_posts"]=>
  300.   bool(true)
  301.   ["read"]=>
  302.   bool(true)
  303.   }
  304.   }
  305.  }
  306.  ?>

追加されてたらOK!

次回は今回追加したroleを指定してユーザー登録する処理をやります。

注意事項

このエントリーはWordPressでブログ以上のことを行う行為を助長するためのものではありませんw

むしろWordpressでブログ以上のことをさせようとするプラグインとか たくさん出ていて、WordPressならなんでも簡単にできちゃうじゃん?みたいなこの空気が恨めしいくらいですから。

もちろんブログの範囲で利用するには、すんばらしいツールなんですけどねぇ。

関連ページ(6)

WEBプログラム覚書2011年6月 7日 04:04

WordPressで会員サイト開発:Roleを指定してユーザー登録編

WordPressで会員サイト開発。Roleを指定してユーザー登録させちゃうよ。いろんなアクションやフィルターが登場するよ。続きを読む >>

WEBプログラム覚書2011年9月 9日 11:30

[WordPress]ユーザー登録が可能か調べる

WordPressでユーザー登録が可能か調べる方法続きを読む >>

WEBプログラム覚書2011年10月 5日 03:49

[WordPress]ハマりポイントget_user_meta()

ローカル環境で動いて本番環境で動かない。ローカル環境と本番環境で$table_prefixが異なる場合などget_user_meta()使ってないか確認し...続きを読む >>

情報備忘録2012年1月 8日 06:05

[WordPress]囲み型ショートコードの改行とかエスケープとか。

囲み型ショートコードのコンテンツを普通に受け取ると、改行があったりセミコロンとかがエスケープされた状態になっている。 JavascriptとかPHPを書く...続きを読む >>

WEBプログラム覚書2012年1月14日 05:55

[WordPress] テンプレートのごちゃごちゃが限界にきたのでラッパー書いてみた。

テンプレートのごちゃごちゃが限界にきたのでラッパー書いてみた。でも僕なんかではなくもっとデキる人が書いたものを使いたいw続きを読む >>

Serverレッスン2012年2月23日 15:26

WordPressで「テーマのインストール」を非表示にする

間違ってクリックしないように、「テーマのインストール」を非表示にしたい!調べてみた。/wp-admin/themes.php77行目    " class...続きを読む >>

コメント

コメントする
Name
Email Address
URL
PAGE RANK
TIME LINE
2012
05.16

XMLHttpRequestってUserAgentを変更できないのかorz

セキュリティ的に禁止してるんだろうけどChromeでは出来ないのか、そもそもJavascriptの仕様として禁止されてるのかは不明。

2012
05.10

[PHP]文字列を1文字ごとタグで囲む

PHPで文字列を1文字ごとタグで囲みたいとき。

2012
04.25

WebフォントとJavascript

WebフォントとjQueryのテキストエフェクトプラグインを試してて気がついたんだけど API経由でWebフォント取得、設定してるとJavascriptの実行が早すぎて困るw

2012
04.12

[Ubuntu11.10]インストール直後にやっておけばよかったこと

Ubuntu 11.10 Desktop 日本語 Remixの場合、ユーザーのホームディレクトリにあるディレクトリ名が 日本語なのでターミナルでディレクトリ移動する場合、非常に扱いづらい。

2012
04.11

シンプルでカスタマイズしやすそうなコンテンツスライダー jQuery Slider2

シンプルでカスタマイズしやすそうなコンテンツスライダー jQuery Slider2

2012
04.06

CORESERVERからロリポのチカッパプランに移転してFTPとおさらばした。

CakePHP2系はPHPのバージョンがシビアなので、CORESERVERからロリポの チカッパプランに移転したんだけど、gitが利用できてなんだか得した気分です。

2012
04.04

[CakePHP2.0.x]FormHelper::input()の出力メモ

FormHelper::input()の出力がどのくらい変更できるかのテスト。

2012
03.28

[CSS]WindowsでLESS更新時に自動でコンパイルする設定方法

Windowsで.lessファイル更新と同時にコンパイルしてCSSを書き出す方法を2つほど。 簡単に開発環境を構築するためUbuntu入れたのに、Windowsの方が簡単だったりするから困るw

2012
03.27

[CSS3/HTML5]ページタイトルとかに使えそうな3Dっぽいテキストエフェクト

Code Padよりタイトルとかに使えそうなテキストエフェクト。それほどゴテゴテしてなくていい感じ。