wordpress主题集成注册邀请码功能的教程

WordPress教程 2761

博客吧前面介绍了阿树工作室的wordpress注册邀请码插件Ashuwp invitaion code,现在分享的非插件实现wordpress注册邀请码功能教程同样来自阿树工作室。通过该教程可以实现在当前使用的wordpress主题中集成注册邀请码功能,其实也就是把插件的代码集成到主题中。不喜欢或者不擅长代码的用户可以直接使用插件实现。

实现思路

1、需要新建一个数据表来保存邀请码,数据表结构如下:

wordpress主题集成注册邀请码功能的教程

  • code – 邀请码
  • max – 邀请码使用次数
  • users – 使用这个验证码的所有用户
  • status – 验证码是否可用

2、后台需要两个页面:邀请码列表、添加邀请码。

3、添加邀请码的时候需要能设置前缀,一次生成多个邀请码,邀请码长度可自定义,每个邀请码使用次数可设置。

实现步骤

1、在当前主题的functions.php文件中添加以下代码:

说明:该代码的作用是建立数据库以及对数据的获取、增加、删除、更改等操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 
 
    <?php
    //第一次启用主题时执行
    function ashuwp_load_theme() {
      global $pagenow;
      if ( is_admin() && 'themes.php' == $pagenow && isset( $_GET['activated'] ) ){
        ashuwp_invitation_code_install();
      }
    }
    add_action( 'load-themes.php', 'ashuwp_load_theme' );
    //建立数据表
    function ashuwp_invitation_code_install(){
      global $wpdb;
      $table_name = $wpdb->prefix . 'invitation_code';
      if( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name ) :
        $sql = " CREATE TABLE `".$wpdb->prefix."invitation_code` (
          `id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
          `code` varchar(40),
          `max` INT NOT NULL,
          `users` varchar(20),
          `status` tinyint
          ) ENGINE = MYISAM DEFAULT CHARSET=utf8;";
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
      endif;
    }
    //插入数据
    function ashuwp_insert_invitation_code( $code, $max = 1, $users, $status){
      global $wpdb;
      if($code==''){
        return false;
      }
      $code = trim($code);
      $code_exists = ashuwp_check_invitation_code($code);
      if(!$code_exists){
        $insert = "insert into ".$wpdb->prefix."invitation_code (code,max,users,status) values( '$code', '$max', '','1')";
        $wpdb->query($insert);
        return true;
      }else{
        return false;
      }
    }
    //检查邀请码是否已存在
    function ashuwp_check_invitation_code( $code ){
      global $wpdb;
      $sql = "select * from ".$wpdb->prefix."invitation_code where code='$code'";
      $result = $wpdb->get_results($sql);
      if(!empty($result)){
        return true;
      }else{
        return false;
      }
    }
    //获取邀请码
    function ashuwp_get_invitation_code($per_page=50, $page=1){
      global $wpdb;
      $page = (int)$page;
      $per_page = (int)$per_page;
      if(!$page){
        $page = 1;
      }
      if(!$per_page){
        $per_page = 50;
      }
      $begin = $per_page*($page-1);
      $end = $per_page*$page;
      $sql = "select * from ".$wpdb->prefix."invitation_code limit $begin,$end";
      $results = $wpdb->get_results($sql,'ARRAY_A');
      return $results;
    }
    //邀请码的删除、启用、禁用。
    function ashuwp_operation_invitation_code( $id, $action ){
      global $wpdb;
      $id = (int)$id;
      if(!$id){
        return false;
      }
      if(!in_array($action,array('delete','deactive','active'))){
        return false;
      }
      if($action =='delete'){
        $sql = "delete from ".$wpdb->prefix ."invitation_code where id='$id'";
      }
      if($action =='deactive'){
        $sql = "update ".$wpdb->prefix ."invitation_code set status=0 where id='$id'";
      }
      if($action =='active'){
        $sql = "update ".$wpdb->prefix ."invitation_code set status=1 where id='$id'";
      }
      $result = $wpdb->query($sql);
      if($result){
        return true;
      }else{
        return false;
      }
    }

2、继续添加以下代码:

说明:这部分代码是制作网站后台部分,包括邀请码列表页面和邀请码增加页面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
 
 
    <?php
    class ashuwp_invitation_code_admin {
      static public $instance;
      public function __construct(){
        add_action( 'admin_menu', array(&$this, 'ashuwp_invitation_code_menu') );
      }
      function ashuwp_invitation_code_menu(){
        add_menu_page( '邀请码', '邀请码', 'manage_options', 'invitation_code', array(&$this, 'invitation_code_list'),'',27);
        add_submenu_page('invitation_code', '添加邀请码', '添加邀请码', 'manage_options', 'invitation_code_add', array(&$this, 'invitation_code_add'));
      }
      function invitation_code_list(){
        if( isset($_GET['code_action']) && in_array($_GET['code_action'],array('delete','deactive','active')) && isset($_GET['code_id']) ){
          $code_id = (int)$_GET['code_id'];
          if(!$code_id){
            return;
          }
          $result = ashuwp_operation_invitation_code( $code_id, $_GET['code_action'] );
        }
        $code_lists = ashuwp_get_invitation_code(999,1);
      ?>
        <div class="wrap">
          <h1 class="wp-heading-inline">邀请码</h1>
          <a href="<?php echo admin_url( 'admin.php?page=invitation_code_add' ); ?>" class="page-title-action">添加</a>
          <hr class="wp-header-end">
          <?php
          if(isset($result)){
            if($result){
          ?>
          <div id="message" class="notice notice-success">操作成功。</div>
          <?php
            }else{
            ?>
            <div id="message" class="notice notice-error">操作失败。</div>
            <?php
            }
          }
          ?>
          <ul class="subsubsub"><li class="all">全部<span class="count">(<?php echo count($code_lists); ?>)</span></ul>
          <table class="wp-list-table widefat fixed">
            <thead>
              <tr>
                <th scope="col">ID</th>
                <th scope="col">邀请码</th>
                <th scope="col">统计(最大使用数/已使用)</th>
                <th scope="col">用户</th>
                <th scope="col">操作</th>
              </tr>
            </thead>
            <tbody>
            <?php
            if($code_lists){
              foreach($code_lists as $code){
                $users = array();
                if(!empty($code['users'])){
                  $users = explode( ',', $code['users'] );
                }
                $used = count($users);
              ?>
              <tr>
                <td><?php echo $code['id']; ?></td>
                <td>
                  <?php echo $code['code']; ?>
                  <?php
                  if(empty($code['status']) || !$code['status']){
                    echo '-已禁用';
                  }
                  ?>
                </td>
                <td>
                  <?php echo $code['max'].'/'.$used; ?>
                </td>
                <td>
                  <?php
                  foreach( $users as $user_id ){
                    $user = get_user_by('id', $user_id);
                    if(!empty($user)){
                    ?>
                    <a href="<?php echo admin_url( 'user-edit.php?user_id='.$user->ID ); ?>"><?php echo $user->user_login; ?></a>,
                    <?php
                    }
                  }
                  ?>
                </td>
                <td>
                  <a href="<?php echo admin_url( 'admin.php?page=invitation_code&code_action=delete&code_id='.$code['id'] ); ?>">删除</a>
                  <?php
                  if(empty($code['status']) || !$code['status']){
                  ?>
                  <a href="<?php echo admin_url( 'admin.php?page=invitation_code&code_action=active&code_id='.$code['id'] ); ?>">启用</a>
                  <?php
                  }else{
                  ?>
                  <a href="<?php echo admin_url( 'admin.php?page=invitation_code&code_action=deactive&code_id='.$code['id'] ); ?>">禁用</a>
                  <?php
                  }
                  ?>
                </td>
              </tr>
              <?php
              }
            }
            ?>
            </tbody>
            <tfoot>
              <tr>
                <th scope="col">ID</th>
                <th scope="col">邀请码</th>
                <th scope="col">统计</th>
                <th scope="col">用户</th>
                <th scope="col">操作</th>
              </tr>
            </tfoot>
          </table>
          <div class="tablenav bottom">
            <div class="tablenav-pages">
              <span class="pagination-links">
              </span>
            </div>
          </div>
        </div>
      <?php
      }
      function invitation_code_add(){
        $data_codes = ashuwp_get_invitation_code(999,1);
        $code_list = array();
        foreach($data_codes as $code){
          $code_list[] = $code['code'];
        }
        if(isset($_REQUEST['submit']) && isset($_REQUEST['ashuwp_invitation_code_field']) && check_admin_referer('ashuwp_invitation_code_action', 'ashuwp_invitation_code_field') ) {
          $code_prefix = '';
          if(!empty($_POST['code_prefix'])){
            $code_prefix = trim($_POST['code_prefix']);
          }
          $code_length = '';
          if(!empty($_POST['code_length'])){
            $code_length = (int)$_POST['code_length'];
          }
          if(!$code_length){
            $code_length = 8;
          }
          $code_number = 1;
          if(!empty($_POST['code_number'])){
            $code_number = (int)$_POST['code_number'];
          }
          if(!$code_number){
            $code_number = 1;
          }
          $code_counter = '';
          if(!empty($_POST['code_counter'])){
            $code_counter = (int)$_POST['code_counter'];
          }
          if(!$code_counter){
            $code_counter = 1;
          }
          $i=1;
          $code_tem = array();
          while ( $i <= $code_number ){
            $tem = strtoupper( $code_prefix . wp_generate_password( $code_length, false ) );
            if(!in_array($tem,$code_list)){
              $i++;
              $code_tem[] = $tem;
              ashuwp_insert_invitation_code( $tem, $code_counter, '', 1);
            }
          }
        }
      ?>
        <div class="wrap">
          <h1 class="wp-heading-inline">添加邀请码</h1>
          <a href="<?php echo admin_url( 'admin.php?page=invitation_code_add' ); ?>" class="page-title-action">添加</a>
          <hr class="wp-header-end">
          <?php
          if(!empty($code_tem)){
          ?>
          <div id="message" class="notice notice-success">
            <p>邀请码添加成功:</p>
            <?php
            foreach($code_tem as $text){
              echo '<p>'.$text.'</p>';
            }
            ?>
          </div>
          <?php
          }
          ?>
          <form action="" method="post">
            <table class="form-table">
              <tbody>
                <tr>
                  <td><label for="code_prefix">邀请码前缀</label></td>
                  <td>
                    <input type="text" id="code_prefix" name="code_prefix" class="regular-text"  value=""/>
                    <p class="description">前缀可不填。</p>
                  </td>
                </tr>
                <tr>
                  <td><label for="code_length">邀请码字符长度</label></td>
                  <td>
                    <input type="text" id="code_length" name="code_length" class="regular-text"  value=""/>
                    <p class="description">字符长度不包括前缀,默认8个字符。</p>
                  </td>
                </tr>
                <tr>
                  <td><label for="code_number">邀请码个数</label></td>
                  <td>
                    <input type="text" id="code_number" name="code_number" class="regular-text" value=""/>
                    <p class="description">本次生成多少个邀请码,默认1个。</p>
                  </td>
                </tr>
                <tr>
                  <td><label for="code_counter">允许使用的次数</label></td>
                  <td>
                    <input type="text" id="code_counter" name="code_counter" class="regular-text"  value=""/>
                    <p class="description">每个邀请码允许使用的次数,默认1次。</p>
                  </td>
                </tr>
              </tbody>
            </table>
            <p class="submit">
              <?php wp_nonce_field( 'ashuwp_invitation_code_action','ashuwp_invitation_code_field' ); ?>
              <input type="submit" name="submit" id="submit" class="button button-primary" value="生成邀请码">
            </p>
          </form>
        </div>
      <?php
      }
    }
    $invitation_code = new ashuwp_invitation_code_admin();

完成效果

1、增加邀请码界面

wordpress主题集成注册邀请码功能的教程

2、邀请码列表界面

wordpress主题集成注册邀请码功能的教程

作者:上面范例代码,仅做参考使用,可根据实际自行优化,具体如何使用邀请码也请自行斟酌。

代码来自http://www.ashuwp.com/courses/supplement/877.html

精品推荐: