wordpress邀请码的实现
有的客户需要邀请码注册,wp自学笔记这就将实现代码贡献出来,在实现邀请码的功能时有参考过一个插件。
思路:
1. 需要新建一个数据表来保存邀请码。
2. 后台需要两个页面:邀请码列表、添加邀请码。
3. 添加邀请码的时候需要能设置前缀,一次生成多个邀请码,邀请码长度可自定义,每个邀请码使用次数可设置。
数据表
code:邀请码、max:邀请码使用次数、users:使用这个验证码的所有用户、status:验证码是否可用。
数据库操作
数据库操作部分代码包括:1. 建立数据库。2.对数据的获取、增加、删除、更改等操作。
- <?php
- //第一次启用主题时执行
- ashuwp_load_theme() {
- $pagenow;
- ( is_admin() && ‘themes.php’ == $pagenow && isset( $_GET[‘activated’] ) ){
- ashuwp_invitation_code_install();
- }
- }
- add_action( ‘load-themes.php’, ‘ashuwp_load_theme’ );
- //建立数据表
- ashuwp_invitation_code_install(){
- $wpdb;
- $table_name = $wpdb->prefix . ‘invitation_code’;
- ( $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;”;
- (ABSPATH . ‘wp-admin/includes/upgrade.php’);
- dbDelta($sql);
- ;
- }
- //插入数据
- ashuwp_insert_invitation_code( $code, $max = 1, $users, $status){
- $wpdb;
- ($code==”){
- false;
- }
- $code = trim($code);
- $code_exists = ashuwp_check_invitation_code($code);
- (!$code_exists){
- $insert = “insert into “.$wpdb->prefix.“invitation_code (code,max,users,status) values( ‘$code’, ‘$max’, ”,’1′)”;
- $wpdb->query($insert);
- true;
- }{
- false;
- }
- }
- //检查邀请码是否已存在
- ashuwp_check_invitation_code( $code ){
- $wpdb;
- $sql = “select * from “.$wpdb->prefix.“invitation_code where code=’$code'”;
- $result = $wpdb->get_results($sql);
- (!($result)){
- true;
- }{
- false;
- }
- }
- //获取邀请码
- ashuwp_get_invitation_code($per_page=50, $page=1){
- $wpdb;
- $page = (int)$page;
- $per_page = (int)$per_page;
- (!$page){
- $page = 1;
- }
- (!$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’);
- $results;
- }
- //邀请码的删除、启用、禁用。
- ashuwp_operation_invitation_code( $id, $action ){
- $wpdb;
- $id = (int)$id;
- (!$id){
- false;
- }
- (!in_array($action,(‘delete‘,’deactive’,’active’))){
- false;
- }
- ($action ==’delete‘){
- $sql = “delete from “.$wpdb->prefix .“invitation_code where id=’$id'”;
- }
- ($action ==’deactive’){
- $sql = “update “.$wpdb->prefix .“invitation_code set status=0 where id=’$id'”;
- }
- ($action ==’active’){
- $sql = “update “.$wpdb->prefix .“invitation_code set status=1 where id=’$id'”;
- }
- $result = $wpdb->query($sql);
- ($result){
- true;
- }{
- false;
- }
- }
网站后台
网站后台部分代码包括:1. 邀请码列表页面。2. 邀请码增加页面。
- <?php
- ashuwp_invitation_code_admin {
- $instance;
- __construct(){
- add_action( ‘admin_menu’, (&$this, ‘ashuwp_invitation_code_menu’) );
- }
- ashuwp_invitation_code_menu(){
- add_menu_page( ‘邀请码’, ‘邀请码’, ‘manage_options’, ‘invitation_code’, (&$this, ‘invitation_code_list’),”,27);
- add_submenu_page(‘invitation_code’, ‘添加邀请码’, ‘添加邀请码’, ‘manage_options’, ‘invitation_code_add’, (&$this, ‘invitation_code_add’));
- }
- invitation_code_list(){
- ( isset($_GET[‘code_action’]) && in_array($_GET[‘code_action’],(‘delete‘,’deactive’,’active’)) && isset($_GET[‘code_id’]) ){
- $code_id = (int)$_GET[‘code_id’];
- (!$code_id){
- ;
- }
- $result = ashuwp_operation_invitation_code( $code_id, $_GET[‘code_action’] );
- }
- $code_lists = ashuwp_get_invitation_code(999,1);
- ?>
- <div =“wrap”>
- <h1 =“wp-heading-inline”>邀请码</h1>
- <a href=“<?php echo admin_url( ‘admin.php?page=invitation_code_add’ ); ?>” =“page-title-action”>添加</a>
- <hr =“wp-header-end”>
- <?php
- (isset($result)){
- ($result){
- ?>
- <div id=“message” =“notice notice-success”>操作成功。</div>
- <?php
- }{
- ?>
- <div id=“message” =“notice notice-error”>操作失败。</div>
- <?php
- }
- }
- ?>
- <ul =“subsubsub”><li =“all”>全部<span =“count”>(<?php echo count($code_lists); ?>)</span></ul>
- <table =“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
- ($code_lists){
- ($code_lists $code){
- $users = ();
- (!($code[‘users’])){
- $users = explode( ‘,’, $code[‘users’] );
- }
- $used = count($users);
- ?>
- <tr>
- <td><?php echo $code[‘id’]; ?></td>
- <td>
- <?php echo $code[‘code’]; ?>
- <?php
- (($code[‘status’]) || !$code[‘status’]){
- echo ‘-已禁用’;
- }
- ?>
- </td>
- <td>
- <?php echo $code[‘max’].’/’.$used; ?>
- </td>
- <td>
- <?php
- ( $users $user_id ){
- $user = get_user_by(‘id’, $user_id);
- (!($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
- (($code[‘status’]) || !$code[‘status’]){
- ?>
- <a href=“<?php echo admin_url( ‘admin.php?page=invitation_code&code_action=active&code_id=’.$code[‘id’] ); ?>”>启用</a>
- <?php
- }{
- ?>
- <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 =“tablenav bottom”>
- <div =“tablenav-pages”>
- <span =“pagination-links”>
- </span>
- </div>
- </div>
- </div>
- <?php
- }
- invitation_code_add(){
- $data_codes = ashuwp_get_invitation_code(999,1);
- $code_list = ();
- ($data_codes $code){
- $code_list[] = $code[‘code’];
- }
- (isset($_REQUEST[‘submit’]) && isset($_REQUEST[‘ashuwp_invitation_code_field’]) && check_admin_referer(‘ashuwp_invitation_code_action’, ‘ashuwp_invitation_code_field’) ) {
- $code_prefix = ”;
- (!($_POST[‘code_prefix’])){
- $code_prefix = trim($_POST[‘code_prefix’]);
- }
- $code_length = ”;
- (!($_POST[‘code_length’])){
- $code_length = (int)$_POST[‘code_length’];
- }
- (!$code_length){
- $code_length = 8;
- }
- $code_number = 1;
- (!($_POST[‘code_number’])){
- $code_number = (int)$_POST[‘code_number’];
- }
- (!$code_number){
- $code_number = 1;
- }
- $code_counter = ”;
- (!($_POST[‘code_counter’])){
- $code_counter = (int)$_POST[‘code_counter’];
- }
- (!$code_counter){
- $code_counter = 1;
- }
- $i=1;
- $code_tem = ();
- ( $i <= $code_number ){
- $tem = strtoupper( $code_prefix . wp_generate_password( $code_length, false ) );
- (!in_array($tem,$code_list)){
- $i++;
- $code_tem[] = $tem;
- ashuwp_insert_invitation_code( $tem, $code_counter, ”, 1);
- }
- }
- }
- ?>
- <div =“wrap”>
- <h1 =“wp-heading-inline”>添加邀请码</h1>
- <a href=“<?php echo admin_url( ‘admin.php?page=invitation_code_add’ ); ?>” =“page-title-action”>添加</a>
- <hr =“wp-header-end”>
- <?php
- (!($code_tem)){
- ?>
- <div id=“message” =“notice notice-success”>
- <p>邀请码添加成功:</p>
- <?php
- ($code_tem $text){
- echo ‘<p>’.$text.'</p>’;
- }
- ?>
- </div>
- <?php
- }
- ?>
- <form action=“” method=“post”>
- <table =“form-table”>
- <tbody>
- <tr>
- <td><label =“code_prefix”>邀请码前缀</label></td>
- <td>
- <input type=“text” id=“code_prefix” name=“code_prefix” =“regular-text” value=“”/>
- <p =“description”>前缀可不填。</p>
- </td>
- </tr>
- <tr>
- <td><label =“code_length”>邀请码字符长度</label></td>
- <td>
- <input type=“text” id=“code_length” name=“code_length” =“regular-text” value=“”/>
- <p =“description”>字符长度不包括前缀,默认8个字符。</p>
- </td>
- </tr>
- <tr>
- <td><label =“code_number”>邀请码个数</label></td>
- <td>
- <input type=“text” id=“code_number” name=“code_number” =“regular-text” value=“”/>
- <p =“description”>本次生成多少个邀请码,默认1个。</p>
- </td>
- </tr>
- <tr>
- <td><label =“code_counter”>允许使用的次数</label></td>
- <td>
- <input type=“text” id=“code_counter” name=“code_counter” =“regular-text” value=“”/>
- <p =“description”>每个邀请码允许使用的次数,默认1次。</p>
- </td>
- </tr>
- </tbody>
- </table>
- <p =“submit”>
- <?php wp_nonce_field( ‘ashuwp_invitation_code_action’,’ashuwp_invitation_code_field’ ); ?>
- <input type=“submit” name=“submit” id=“submit” =“button button-primary” value=“生成邀请码”>
- </p>
- </form>
- </div>
- <?php
- }
- }
- $invitation_code = 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/
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END