Ashuwp invitaion code - 邀請(qǐng)碼生成器

    簡(jiǎn)單實(shí)用的wordpress邀請(qǐng)碼、注冊(cè)碼生成器

    Ashuwp_Invitation_Code是一個(gè)wordpress插件,它有助于為您的網(wǎng)站添加邀請(qǐng)代碼。 wordpress用戶還可以配合一些日志限制訪問(wèn)代碼來(lái)做一個(gè)付費(fèi)閱讀網(wǎng)站。

    要使用此插件需要開啟wordpress的注冊(cè)功能。

    代碼版

    純代碼實(shí)現(xiàn)邀請(qǐng)碼 ?? 的思路:

    1. 需要新建一個(gè)數(shù)據(jù)表來(lái)保存邀請(qǐng)碼。
    2. 后臺(tái)需要兩個(gè)頁(yè)面:邀請(qǐng)碼列表、添加邀請(qǐng)碼。
    3. 添加邀請(qǐng)碼的時(shí)候需要能設(shè)置前綴,一次生成多個(gè)邀請(qǐng)碼,邀請(qǐng)碼長(zhǎng)度可自定義,每個(gè)邀請(qǐng)碼使用次數(shù)可設(shè)置。

    數(shù)據(jù)表

    code:邀請(qǐng)碼、max:邀請(qǐng)碼使用次數(shù)、users:使用這個(gè)驗(yàn)證碼的所有用戶、status:驗(yàn)證碼是否可用。

    Ashuwp invitaion code - 邀請(qǐng)碼生成器

    數(shù)據(jù)庫(kù)操作

    數(shù)據(jù)庫(kù)操作部分代碼包括:1. 建立數(shù)據(jù)庫(kù)。2.對(duì)數(shù)據(jù)的獲取、增加、刪除、更改等操作。

    <?php
    //第一次啟用主題時(shí)執(zhí)行
    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' );
    //建立數(shù)據(jù)表
    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;
    }
    //插入數(shù)據(jù)
    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;
      }
    }
    //檢查邀請(qǐng)碼是否已存在
    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;
      }
    }
    //獲取邀請(qǐng)碼
    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;
    }
    //邀請(qǐng)碼的刪除、啟用、禁用。
    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;
      }
    }

    網(wǎng)站后臺(tái)
    網(wǎng)站后臺(tái)部分代碼包括:1. 邀請(qǐng)碼列表頁(yè)面。2. 邀請(qǐng)碼增加頁(yè)面。

    <?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( '邀請(qǐng)碼', '邀請(qǐng)碼', 'manage_options', 'invitation_code', array(&$this, 'invitation_code_list'),'',27);
        add_submenu_page('invitation_code', '添加邀請(qǐng)碼', '添加邀請(qǐng)碼', '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">邀請(qǐng)碼</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">邀請(qǐng)碼</th>
                <th scope="col">統(tǒng)計(jì)(最大使用數(shù)/已使用)</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">邀請(qǐng)碼</th>
                <th scope="col">統(tǒng)計(jì)</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">添加邀請(qǐng)碼</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>邀請(qǐng)碼添加成功:</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">邀請(qǐng)碼前綴</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">邀請(qǐng)碼字符長(zhǎng)度</label></td>
                  <td>
                    <input type="text" id="code_length" name="code_length" class="regular-text"  value=""/>
                    <p class="description">字符長(zhǎng)度不包括前綴,默認(rèn)8個(gè)字符。</p>
                  </td>
                </tr>
                <tr>
                  <td><label for="code_number">邀請(qǐng)碼個(gè)數(shù)</label></td>
                  <td>
                    <input type="text" id="code_number" name="code_number" class="regular-text" value=""/>
                    <p class="description">本次生成多少個(gè)邀請(qǐng)碼,默認(rèn)1個(gè)。</p>
                  </td>
                </tr>
                <tr>
                  <td><label for="code_counter">允許使用的次數(shù)</label></td>
                  <td>
                    <input type="text" id="code_counter" name="code_counter" class="regular-text"  value=""/>
                    <p class="description">每個(gè)邀請(qǐng)碼允許使用的次數(shù),默認(rèn)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="生成邀請(qǐng)碼">
            </p>
          </form>
        </div>
      <?php
      }
    }
    $invitation_code = new ashuwp_invitation_code_admin();

    效果展示

    1. 增加邀請(qǐng)碼

    Ashuwp invitaion code - 邀請(qǐng)碼生成器

    2. 邀請(qǐng)碼列表

    Ashuwp invitaion code - 邀請(qǐng)碼生成器

    結(jié)束語(yǔ)

    上面范例代碼,僅做參考使用,可根據(jù)實(shí)際自行優(yōu)化,具體如何使用邀請(qǐng)碼也請(qǐng)自行斟酌。

    相關(guān)推薦

    Ashuwp invitaion code - 邀請(qǐng)碼生成器-Npcink
    Ashuwp invitaion code - 邀請(qǐng)碼生成器-Npcink

    隱藏分類以及文章但登陸后可見 - WordPress教程

    下載權(quán)限

    查看
    • 免費(fèi)下載
      評(píng)論并刷新后下載
      登錄后下載

    查看演示

    • {{attr.name}}:
    您當(dāng)前的等級(jí)為
    登錄后免費(fèi)下載登錄 小黑屋反思中,不準(zhǔn)下載! 評(píng)論后刷新頁(yè)面下載評(píng)論 支付以后下載 請(qǐng)先登錄 您今天的下載次數(shù)(次)用完了,請(qǐng)明天再來(lái) 支付積分以后下載立即支付 支付以后下載立即支付 您當(dāng)前的用戶組不允許下載升級(jí)會(huì)員
    您已獲得下載權(quán)限 您可以每天下載資源次,今日剩余
    插件

    Option Tree - Wordpress設(shè)置框架

    2020-1-7 19:42:40

    插件

    User Submitted Posts -前端編輯器

    2020-1-8 23:55:12

    ??
    Npcink上的部份代碼及教程來(lái)源于互聯(lián)網(wǎng),僅供網(wǎng)友學(xué)習(xí)交流,若您喜歡本文可附上原文鏈接隨意轉(zhuǎn)載。
    無(wú)意侵害您的權(quán)益,請(qǐng)發(fā)送郵件至 1355471563#qq.com 或點(diǎn)擊右側(cè) 私信:Muze 反饋,我們將盡快處理。
    0 條回復(fù) A文章作者 M管理員
      暫無(wú)討論,說(shuō)說(shuō)你的看法吧
    ?
    個(gè)人中心
    購(gòu)物車
    優(yōu)惠劵
    今日簽到
    有新私信 私信列表
    搜索
    主站蜘蛛池模板: 亚洲永久无码3D动漫一区| 无码av不卡一区二区三区| 国产一区二区成人| 日韩AV在线不卡一区二区三区| 国产在线精品一区二区| 国产色情一区二区三区在线播放| 97一区二区三区四区久久 | 无码人妻一区二区三区精品视频 | 国产色情一区二区三区在线播放| 日韩免费无码视频一区二区三区| 中文字幕亚洲乱码熟女一区二区| 国产成人无码一区二区三区在线 | 国产suv精品一区二区6| 麻豆文化传媒精品一区二区| 国产成人精品一区二区A片带套| 亚洲国产精品无码久久一区二区| 小泽玛丽无码视频一区| 无码精品不卡一区二区三区| 亚洲男女一区二区三区| 色国产精品一区在线观看| 人妻无码一区二区视频| 国产AV天堂无码一区二区三区| 日韩精品一区二区三区大桥未久| 无码人妻精品一区二区蜜桃百度| 日本v片免费一区二区三区| 香蕉久久AⅤ一区二区三区| 无码少妇一区二区浪潮av| 精品欧美一区二区在线观看| 亚洲日韩国产一区二区三区在线 | 国产一区二区电影| 国产91一区二区在线播放不卡 | 国产伦精品一区二区三区在线观看 | 亚洲中文字幕在线无码一区二区| 国产AV午夜精品一区二区三| 久久一区二区精品综合| 亚洲午夜电影一区二区三区| 日本精品无码一区二区三区久久久| 国产一区二区好的精华液| 日本一区二区三区在线网| 国产在线精品一区二区中文| 精品无码一区二区三区在线|