Skip to main content
We are Brand SEO Beijing serving international business, your marketing partner, Contact us by mi@mgsh.com.cn

Website construction to realize the function of invitation code

Some customers need to register with an invitation code, and Ashu Studio will contribute the implementation code. When implementing the function of the invitation code, a plug-in was used as a reference.
Ideas:
1. You need to create a new data table to save the invitation code.
2. Two pages are required in the background: Invitation Code List and Add Invitation Code.
3. When adding an invitation code, you need to be able to set a prefix, generate multiple invitation codes at one time, the length of the invitation code can be customized, and the number of times each invitation code can be used can be set.
data sheet
code: invitation code, max: the number of times the invitation code is used, users: all users who use this verification code, status: whether the verification code is available.
yqm data 1-brand-SEO-public opinion-optimization-米国生活
yqm data-brand-SEO-public opinion-optimization-米国生活database operations
Part of the database operation code includes: 1. Establish a database. 2. Data acquisition, addition, deletion, modification and other operations.

<?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;
  }
}

Website background
Some codes in the background of the website include: 1. Invitation code list page. 2. Add invitation code page.

<?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%20echo%20admin_url(%20'admin.php?page=invitation_code_add'%20);%20?>" 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%20echo%20admin_url(%20'user-edit.php?user_id='.$user->ID%20);%20?>"><?php echo $user->user_login; ?></a>,
                <?php
                }
              }
              ?>
            </td>
            <td>
              <a href="<?php%20echo%20admin_url(%20'admin.php?page=invitation_code&code_action=delete&code_id='.$code['id']%20);%20?>">删除</a>
              <?php
              if(empty($code['status']) || !$code['status']){
              ?>
              <a href="<?php%20echo%20admin_url(%20'admin.php?page=invitation_code&code_action=active&code_id='.$code['id']%20);%20?>">启用</a>
              <?php
              }else{
              ?>
              <a href="<?php%20echo%20admin_url(%20'admin.php?page=invitation_code&code_action=deactive&code_id='.$code['id']%20);%20?>">禁用</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 . 

Show results
1. Add an invitation code
yqm add-brand-SEO-public opinion-optimization-米国生活
2. Invitation code list
qzm view-brand-SEO-public opinion-optimization-米国生活
Conclusion
The above sample code is for reference only and can be optimized according to the actual situation. Please consider how to use the invitation code by yourself.
wordpress.org/plugins/ashuwp-invitaion-code/

Back to Top