WordPress User Model for CodeIgniter
The User Model class bellow is based in WordPress user functions, which makes it easier for WordPress developers to apply the same functions and syntax to their CodeIgniter Web Applications.
- The database structure is based in WordPress tables using “ci_” prefix.
- Uses the same password hashing of WordPress (PHPpass)
- Authentication function included: Login, Register
- Helper functions: update_usermeta, add_usermeta, delete_usermeta, add_user, delete_user, update_user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | <?php /** * User_model * * @author Carl Victor Fontanos * @authorurl www.carlofontanos.com * @version 1.0 * */ if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class User_model extends CI_Model { public function __construct() { parent::__construct(); } public function login($user_email, $user_pass) { $query_password = $this->db->get_where('ci_users', array('user_email' => $user_email), 1, 0); $user = $query_password->row(); if($this->passwordhash->CheckPassword($user_pass, $user->user_pass)) { $newdata = array( 'ID' => $user->ID, 'logged_in' => TRUE ); $this->session->set_userdata($newdata); return true; } return false; } public function add_user($username, $email, $password) { $user_data = array( 'user_login' => $username, 'user_email' => $email, 'user_pass' => $this->passwordhash->HashPassword($password), 'user_registered' => date("Y-m-d H:i:s", time()), 'user_nicename' => url_title($email, 'dash', TRUE), ); $cleaned_data = $this->security->xss_clean($user_data); $this->db->insert('ci_users', $user_data); $this->db->select('ID'); $user_check = $this->db->get_where('ci_users', array('user_email' => $email), 1, 0); if($user_check->num_rows() > 0){ $user = $user_check->row(); return $user->ID; } else { return false; } } public function add_user_meta($user_id, $meta_key, $meta_value) { $user_metadata = array( 'user_id' => $user_id, 'meta_key' => $meta_key, 'meta_value' => $meta_value ); $cleaned_data = $this->security->xss_clean($user_metadata); $this->db->insert('ci_usermeta', $cleaned_data); $add_meta_check = $this->db->get_where('ci_usermeta', array('user_id' => $user_id, 'meta_key' => $meta_key, 'meta_value' => $meta_value), 1, 0); if($add_meta_check->num_rows() > 0){ return true; } else { return false; } } public function update_user_meta($user_id, $meta_key, $meta_value) { $meta_exits = $this->db->get_where('ci_usermeta', array('user_id' => $user_id, 'meta_key' => $meta_key), 1, 0); if($meta_exits->num_rows() > 0){ $user_metadata = array( 'meta_value' => $meta_value ); $cleaned_data = $this->security->xss_clean($user_metadata); $this->db->where(array('user_id' => $user_id, 'meta_key' => $meta_key)); $this->db->update('ci_usermeta', $cleaned_data); $update_meta_check = $this->db->get_where('ci_usermeta', array('user_id' => $user_id, 'meta_key' => $meta_key, 'meta_value' => $meta_value), 1, 0); if($update_meta_check->num_rows() > 0){ return true; } else { return false; } } else { self::add_user_meta($user_id, $meta_key, $meta_value); return true; } } public function delete_user_meta($user_id, $meta_key = '') { if(!empty($meta_key)){ $user_metadata = array( 'user_id' => $user_id, 'meta_key' => $meta_key ); } else { $user_metadata = array( 'user_id' => $user_id ); } $cleaned_data = $this->security->xss_clean($user_metadata); $this->db->where($cleaned_data); $this->db->delete('ci_usermeta'); $delete_meta_check = $this->db->get_where('ci_usermeta', $user_metadata, 1, 0); if($delete_meta_check->num_rows() == 0){ return true; } else { return false; } } public function get_user_meta($user_id, $key = '', $single = TRUE) { $where = ''; if($key){ $where .= ' AND meta_key = "'.$key.'"'; } $user_meta = $this->db->query('SELECT * FROM ci_usermeta WHERE user_id = ?'.$where, array($user_id) ); if($single){ $row = $user_meta->row(); if($row){ return $row->meta_value; } else { return ''; } } else { return $user_meta->result(); } } public function update_user($userdata_array) { extract($userdata_array); $data = ''; if(!empty($user_login)){ $data['user_login'] = $user_login; } if(!empty($user_email)){ $data['user_email'] = $user_email; $data['user_nicename'] = url_title($user_email, 'dash', TRUE); } if(!empty($user_pass)){ $data['user_pass'] = $this->passwordhash->HashPassword($user_pass); } $cleaned_data = $this->security->xss_clean($data); $this->db->update('ci_users', $cleaned_data); } public function delete_user($user_id) { $this->db->delete('ci_users', array('ID' => $user_id)); self::delete_user_meta($user_id); $delete_check = $this->db->query('SELECT * from ci_users u LEFT JOIN ci_usermeta um ON u.ID = um.user_id WHERE u.ID = ?', array($user_id) ); if($delete_check->num_rows() == 0){ return true; } else { return false; } } } |
Usage
In your controller “___construct” function, load the user_model:
1 | $this->load->model('user_model'); |
Then in your PAGE controller, assign the model into a variable:
1 | $data['user'] = $this->user_model; |
In your View, you can call the functions like this:
1 | <?php echo $user->get_user_meta($current_user->ID, 'first_name'); ?> |
Do you need help with a project? or have a new project in mind that you need help with?
Contact Me