LoginSignup
0
6

More than 3 years have passed since last update.

WordpressのWP-Membersプラグインで会員サイトを作ってみる

Last updated at Posted at 2020-12-15

プラグインWP-members

参考:会員サイトが簡単に作成できるWP-Memberを実装してみた!
http://creatornote.nakweb.com/%E4%BC%9A%E5%93%A1%E3%82%B5%E3%82%A4%E3%83%88%E3%81%8C%E7%B0%A1%E5%8D%98%E3%81%AB%E4%BD%9C%E6%88%90%E3%81%A7%E3%81%8D%E3%82%8Bwp-member%E3%82%92%E5%AE%9F%E8%A3%85%E3%81%97%E3%81%A6%E3%81%BF%E3%81%9F/

ショートコード

hoge.txt
[wpmem_form login]
//ログインフォームを出力。

[wpmem_form register]
//新規登録フォームを出力。

[wpmem_profile]
//ユーザープロフィールを出力。

[wpmem_form user_edit]
//ユーザープロフィールの編集フォームを出力。

[wpmem_logout]
//ログアウトページの出力

[wpmem_form password]
//パスワードの変更・リセット

[wpmem_form forgot_username]
//ユーザー名を忘れた場合の回復するためのEメール入力フォーム。

[wpmem_logged_in]ログインユーザーのみ[/wpmem_logged_in]
//ショートコードで囲んだテキストがログインユーザーのみに表示

[wpmem_logged_out]ログアウトユーザーのみ[/wpmem_logged_out]
//ショートコードで囲んだテキストがログアウトユーザーのみに表示

[wpmem_field user_login]
//ユーザー名を表示

[wpmem_avatar]
//プロフィール画像を表示

[wpmem_field user_email]
//Eメールアドレスを表示

[wpmem_login_link]ログイン[/wpmem_login_link]
//ログインページへのリンクとショートコードで囲んだリンクテキスト

[wpmem_reg_link]新規登録[/wpmem_reg_link]
//新規登録ページへのリンクとショートコードで囲んだリンクテキスト。

ユーザー関連の処理

hoge.php
<?php 

  //ユーザー情報を取得
  $user = wp_get_current_user();
  echo $user->ID; //ユーザーID
  echo $user->user_login; //ログインID
  echo $user->user_nicename; //サニタイズ後のログインID
  echo $user->user_email; //登録メールアドレス
  echo $user->user_status; //ユーザーステータス
  echo $user->display_name; //WordPress上の表示名

  //ログインされていたら
  if(is_user_logged_in()){
    //ここに処理を記述
  }

  // 権限グループ【管理者】であれば
  if (current_user_can('administrator')) {
    //ここに処理を記述
  }

  // 権限グループ【購読者】であれば
  if (current_user_can('subscriber')) {
    //ここに処理を記述
  }


?>

定義したユーザーフィールドの値を取得する

hoge.php
        $user = wp_get_current_user();
        $UID = $user -> ID;
        $surl= get_the_author_meta( 'surl', $UID );
        //surlは自由に定義したもの

会員(購読者)の場合は管理画面への侵入不可とメニューバーを表示させない

function.php

// 権限グループ【購読者】であれば管理バーを非表示にする
if (current_user_can('subscriber')) {
add_filter( 'show_admin_bar', '__return_false' );
}


// 会員を管理画面に入れない
function subscriber_go_to_home( $user_id ) {
  $user = get_userdata( $user_id );
  if ( !$user->has_cap( 'edit_posts' ) ) {
    wp_redirect( get_home_url() );
    exit();
  }
}
add_action( 'auth_redirect', 'subscriber_go_to_home' );

ユーザー(会員)情報はどこに保存されるのか

ユーザー情報は「xxxx_users」テーブルに保存され、追加フィールドは「xxxx_usermeta」に保存される。

「xxxx_users」テーブル

image.png
IDとuser_login の各項目がキーになっている

「xxxx_usermeta」テーブル

image.png

xxxx_usersテーブルのIDがキーとなってテーブルが構成されている。

ショートコードを使ってphpファイルを呼び出す方法

funciton.phpでの設定

function.php
//ショートコードを使ったphpファイルの呼び出し方法
function Include_my_php($params = array()) {
    extract(shortcode_atts(array(
        'file' => 'default'
    ), $params));
    ob_start();
    include(STYLESHEETPATH . "/$file.php");
    return ob_get_clean();
}
add_shortcode('myphp', 'Include_my_php');

投稿ページ・固定ページからショートコードで呼び出す

このケースではfunction.phpと同じ階層にmemeberslogin.phpファイルを作成してそれを呼び出している

hoge.html
[myphp file='memberslogin']
0
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
6