-
Plug in introduction: details
Code version
-
Original reference: details
-
You need to create a new data table to save the invitation code. -
The background needs two pages: invitation code list and add invitation code. -
When you add an invitation code, you need to be able to set a prefix, generate multiple invitation codes at a time, customize the length of the invitation code, and set the number of times each invitation code is used.
<? php //Executed the first time the theme is enabled 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' ); //Create data table 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; } //Insert Data 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; } } //Check whether the invitation code already exists 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; } } //Get invitation code 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; } //Delete, enable and disable invitation codes. 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; } }
<? 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 ('invite code ','invite code','manage_options','inventition_code ', array (&$this,'inventition_code_list'), '', 27); Add_submenu_page ('invitation_code', 'add invitation code', 'add 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">Invitation code</h1> <a href="<? Php echo admin_url ('admin. php? Page=invitation_code_add ');?>" class="page title action">Add</a> <hr class="wp-header-end"> <? php if(isset($result)){ if($result){ ?> <div id="message" class="notice notice success">The operation is successful</ div> <? php }else{ ?> <div id="message" class="notice notice error">The operation failed</ div> <? php } } ?> <ul class="subsub"><li class="all">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">Invitation code</th> <th scope="col">Statistics (maximum usage/used)</th> <th scope="col">Users</th> <th scope="col">Operation</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 '- Disabled'; } ?> </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']);?>">Delete</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']);?>">Enable</a> <? php }else{ ?> <a href="<? Php echo admin_url ('admin. php? Page=invitation_code&code_action=inactive&code_id='. $code ['id']);?>">Disable</a> <? php } ?> </td> </tr> <? php } } ?> </tbody> <tfoot> <tr> <th scope="col">ID</th> <th scope="col">Invitation code</th> <th scope="col">Statistics</th> <th scope="col">Users</th> <th scope="col">Operation</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">Add invitation code</h1> <a href="<? Php echo admin_url ('admin. php? Page=invitation_code_add ');?>" class="page title action">Add</a> <hr class="wp-header-end"> <? php if(!empty($code_tem)){ ?> <div id="message" class="notice notice-success"> <p>Invitation code added successfully:</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">Invitation code prefix</label></td> <td> <input type="text" id="code_prefix" name="code_prefix" class="regular-text" value=""/> <p class="description">The prefix is optional</ p> </td> </tr> <tr> <td><label for="code_length">Character length of invitation code</label></td> <td> <input type="text" id="code_length" name="code_length" class="regular-text" value=""/> <p class="description">The character length does not include the prefix. It is 8 characters by default</ p> </td> </tr> <tr> <td><label for="code_number">Number of invitation codes</label></td> <td> <input type="text" id="code_number" name="code_number" class="regular-text" value=""/> <p class="description">How many invitation codes are generated this time? The default is 1</ p> </td> </tr> <tr> <td><label for="code_counter">Number of times allowed to use</label></td> <td> <input type="text" id="code_counter" name="code_counter" class="regular-text" value=""/> <p class="description">The number of times each invitation code is allowed to be used. It is 1 by default</ 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="Generate invitation code"> </p> </form> </div> <? php } } $invitation_code = new ashuwp_invitation_code_admin();
Related recommendations
Download permission
-
¥ Free download Download after reviewing and refreshing Download after login
View Demo
-
{{attr.name}}: