wordpress邀请码的实现

有的客户需要邀请码注册,阿树工作室这就将实现代码贡献出来,在实现邀请码的功能时有参考过一个插件。

思路:

1. 需要新建一个数据表来保存邀请码。

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

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

数据表

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

数据库操作

数据库操作部分代码包括:1. 建立数据库。2.对数据的获取、增加、删除、更改等操作。

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

网站后台
网站后台部分代码包括:1. 邀请码列表页面。2. 邀请码增加页面。

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

效果展示

1. 增加邀请码

2. 邀请码列表

结束语

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

 

后记

阿树得空的时候,写了一个验证码插件,发布在了github上,有需要的朋友可直接下载使用。

https://github.com/ashuwp/Ashuwp_Invitation_Code

后记的后记

阿树将插件发布在了worpdress官网,在wordpress官网搜索ashuwp-invitaion-code即可找到。

地址:https://wordpress.org/plugins/ashuwp-invitaion-code/

本篇教程之前的几篇教程是

本篇教程之后的几篇教程是

没有找到你要找的内容?你可以通过搜索你要找的内容,或者给我们留言。

已有20条评论

hui进行回复 取消回复