Skip to content

Instantly share code, notes, and snippets.

@edirpedro
Created September 23, 2017 00:08
Show Gist options
  • Save edirpedro/528c671b31557be8769620337e2b44a0 to your computer and use it in GitHub Desktop.
Save edirpedro/528c671b31557be8769620337e2b44a0 to your computer and use it in GitHub Desktop.
A solution if you need to separate some custom fields data from the table wp_usermeta for any reason.
<?php
/*
* Custom WP Usermeta Table
*
* Version: 1.0
* Author: Edir Pedro
* Website: https://gist.github.com/edirpedro/528c671b31557be8769620337e2b44a0
*
* How to use?
*
* 1) Duplicate the original "wp_usermeta" table and set a suffix like "wp_usermeta_tablename".
* 2) Add the new table: CustomUsermetaTable::add("tablename", "fieldprefix");
* 3) Remember to use ALL your custom fields name like "fieldprefix_fieldname"
* to automatically redirect the data to your new table.
*
*/
class CustomUsermetaTable {
static private $tables = array();
// Add new table setting the table suffix and the field prefix
static public function add($name, $prefix) {
global $wpdb;
// Adding new table
self::$tables[] = array(
'name' => $wpdb->usermeta . '_' . $name,
'prefix' => $prefix . '_'
);
// Hooks
add_filter('add_user_metadata', array(__CLASS__, 'update_metadata'), 0, 4);
add_filter('update_user_metadata', array(__CLASS__, 'update_metadata'), 0, 4);
add_filter('get_user_metadata', array(__CLASS__, 'get_metadata'), 0, 3);
add_filter('delete_user_metadata', array(__CLASS__, 'delete_metadata'), 0, 3 );
}
// Return a table if the prefix exists in custom data
static private function get_table($meta_key) {
foreach(self::$tables as $key => $table) {
$prefix = $table['prefix'];
if(strpos($meta_key, $prefix) === 0 || strpos($meta_key, "_$prefix") === 0) // Checking WP and ACF extra field
return self::$tables[$key];
}
return false;
}
// Update or Add the custom data
static public function update_metadata($check, $user_id, $meta_key, $meta_value) {
global $wpdb;
$table = self::get_table($meta_key);
if(!empty($table)) {
$table = $table['name'];
$data = array(
'user_id' => $user_id,
'meta_key' => $meta_key,
'meta_value' => maybe_serialize($meta_value)
);
$query = $wpdb->prepare("SELECT umeta_id FROM $table WHERE meta_key = %s AND user_id = %d", $meta_key, $user_id);
$id = $wpdb->get_var($query);
if(empty($id))
return $wpdb->insert($table, $data);
else
return $wpdb->update($table, $data, array('umeta_id' => $id));
}
return $check;
}
// Get custom data
static public function get_metadata($check, $user_id, $meta_key) {
global $wpdb;
$table = self::get_table($meta_key);
if(!empty($table)) {
$table = $table['name'];
$query = $wpdb->prepare("SELECT meta_value FROM $table WHERE meta_key = %s AND user_id = %d", $meta_key, $user_id);
return $wpdb->get_col($query);
}
return $check;
}
// Delete custom data
static public function delete_metadata($check, $user_id, $meta_key) {
global $wpdb;
$table = self::get_table($meta_key);
if(!empty($table)) {
return $wpdb->delete($table['name'], array('meta_key' => $meta_key, 'user_id' => $user_id));
}
return $check;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment