Because Google tells me that a lot of people have viewed this page, I’ve added some notes in hopes that it will make more sense or be more useful to you. Cheers!
I dunno. Something about transforming the data returned from get_user_meta( user_id )
into something tangible that I wanted to use. Someone might find some use for this for some reason. I was mostly just trying to get to the serialized data in the wp_capabilities
field of the wp_usermeta
database table. It’s garbage code, but I figured out how to isolate the data needed.
Since it’s in a foreach()
loop, one might find it useful for debugging something in the wp_usermeta
table.
If you want to test this, you can probably do so by adding it temporarily to your theme’s functions.php
file. If you don’t know how to do that, just search for “add custom action to theme functions.php” I’ve wrapped it in a function for that purpose (i.e. so someone might copy/ paste into the functions.php file), but I haven’t tested it like that– this is a snippet of code from a custom plugin I was working on, so ….
You’ll also need to get the $user_id
value somehow, and it’s expected to come from an instance of the WP_User
object, where the $wpdb
query selected the default fields from all rows (e.g. 'SELECT * FROM …'
) in the wp_users
and wp_usermeta
tables, plus a few additional rows (as created by adding data to 'meta_value'
and 'meta_key'
corresponding to a unique 'user_id'
) for testing the specific forms and data I was working with. See SQL further below.
<?php // begin edit for testing add_action('wp_footer','process_usermeta'); function process_usermeta() { // end edit for testing $usermeta = get_user_meta( $user_id ); echo '<div class="card">'; foreach( $usermeta as $key => $value ) { $v = (is_array($value)) ? implode(', ', $value) : $value; $span_open = ''; $span_close = ''; if($key == 'session_tokens'){ $v = ''; } if($key == 'description'){ $v = ''; } if($key == 'wp_capabilities'){ $v = unserialize($v); $v = array_keys($v); $vstring = ''; foreach($v as $varray){ $vstring .= json_encode($varray) ; } $v = $vstring; } if($key == 'green_key'){ $span_open = '<span class="green">'; $span_close = '</span>'; } if($key == 'blue_key'){ $span_open = '<span class="blue">'; $span_close = '</span>'; } echo '<p><strong>'.$span_open.$key.$span_close. '</strong>: ' . $v . '</p>'; } echo '</div><br>'; } // process_usermeta() closing bracket ?>
You’ll have to convert the results of this query into a single row result, for the value $user_id
in the first row of the function. My SQL query looks like this:
$user_query = 'SELECT DISTINCT wpu.ID, wpu.user_email, wpu.display_name, wpu.user_login, wpm.meta_value wpmmv, wpm2.meta_value wpm2mv FROM wp_users wpu LEFT JOIN wp_usermeta wpm ON wpm.user_id = wpu.ID LEFT JOIN wp_usermeta wpm2 ON wpm2.user_id = wpu.ID WHERE wpm.meta_key LIKE "one_of_many_usergroups" AND wpm2.meta_key LIKE "number_unique_to_one_userid" ORDER BY wpmmv ASC';
Leave a Reply