Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to display user meta in profile page? WordPress

  • SOLVED

Hello,

I am using the gravity forms user registration plugin.

This plugin is generating custom user meta.

This is how my meta strings appear in gravity forms...

* Title
* Publication / Company
* Job Title
* Publication Location
* Language

But how do you find out what the meta key is for these?

And how do you get them to display in the user profile page?


Many Thanks
Josh


Answers (3)

2012-01-27

John Cotton answers:

If you're happy with the info appearing in text boxes under contact info on the dashboard profile, then this will do it:


function my_user_contactmethods($user_contactmethods) {
// You can get rid of ones you don't want
unset($user_contactmethods['jabber']);
unset($user_contactmethods['yim']);
unset($user_contactmethods['aim']);

// And add any news ones. The array key is the meta key name, the text
// is however you want it labelled.
$user_contactmethods['skype'] = __('Skype');

return $user_contactmethods;
}
add_filter( 'user_contactmethods', 'my_user_contactmethods');


It's possible to do a more customised presentation of user meta on the profile page, but it's more complicated. The code above may well suffice.


Josh Cranwell comments:

Ok thats, cool but how do you find out the custom meta prefix's?

Gravity forms has generated my custom user meta, but I don't know what the prefix's are.

The problem is I've tried so many user registration plugins, and finally found the winning plugin...

but for all I know I could be on custom_usermeta_24 by now.


John Cotton comments:

Stick this code somewhere you can see the output:

$meta = get_user_meta($user_id); print_r($meta);

You can get a user id by looking at the url in the dashboard profile page.

The code above will output all the meta data for the user and you will be able to see what each is called.


Josh Cranwell comments:

Sorry I'm very tired and I not php savy

So your saying if I put this anywhere in my theme so I can see an output, this will show the current logged in user meta data?

<?php $meta = get_user_meta($user_id); print_r($meta); ?>


John Cotton comments:

This

<?php $user_id = 7; $meta = get_user_meta($user_id); print_r($meta); ?>

will show you the user meta of which ever user you set $user_id to.

This

<?php global $current_use; $meta = get_user_meta($current_user->ID); print_r($meta); ?>
will show the current logged in user meta data.

Stick the code at the end of you header.php file and it will appear at the top of your page.


Josh Cranwell comments:

Array ( [Job Title] => Array ( [0] => Designer ) [Publication Location] => Array ( [0] => United Kingdom ) [authentication] => Array ( [0] => 1 ) [Title] => Array ( [0] => Mr ) [Publication / Company] => Array ( [0] => Motocom Digital ) [entry_id] => Array ( [0] => 5 ) [wp_user_level] => Array ( [0] => 0 ) [show_admin_bar_front] => Array ( [0] => true ) [wp_capabilities] => Array ( [0] => a:1:{s:5:"media";s:1:"1";} ) [use_ssl] => Array ( [0] => 0 ) [rich_editing] => Array ( [0] => true ) [comment_shortcuts] => Array ( [0] => false ) [description] => Array ( [0] => ) [last_name] => Array ( [0] => Cranwell ) [nickname] => Array ( [0] => [email protected] ) [first_name] => Array ( [0] => Josh ) [Language] => Array ( [0] => English ) [admin_color] => Array ( [0] => fresh ) )

This what has outputted. So are my meta keys exactly the same as the name of the my field?

Array (
[Job Title] => Array ( [0] => Designer )
[Publication Location] => Array ( [0] => United Kingdom )
[authentication] => Array ( [0] => 1 )
[Title] => Array ( [0] => Mr )
[Publication / Company] => Array ( [0] => Motocom Digital )


So is how would I get this to display admin user profile page? And be able to edit it?

Is it a lengthy process?

Thanks


Josh Cranwell comments:

<blockquote>So is how would I get this to display admin user profile page? And be able to edit it?</blockquote>

I meant...

How would I get this to display admin user profile page? And be able to edit, like you can with other profile information?


John Cotton comments:

Stick this in you functions.php file:



function my_user_contactmethods($user_contactmethods) {
// You can get rid of ones you don't want
unset($user_contactmethods['jabber']);
unset($user_contactmethods['yim']);
unset($user_contactmethods['aim']);

// And add any news ones. The array key is the meta key name, the text
// is however you want it labelled.
$user_contactmethods['Job Title'] = __('Job Title');
$user_contactmethods['Publication Location'] = __('Publication Location');
$user_contactmethods['Title'] = __('Title');
// etc for each field you want to appear

return $user_contactmethods;
}
add_filter( 'user_contactmethods', 'my_user_contactmethods');


Josh Cranwell comments:

A gentleman and a scholar, thank you!!!!!!!!!!

Worked a treat.

Cheers John


John Cotton comments:

My pleasure!

I see that this question you asked:
http://wpquestions.com/question/showLoggedIn/id/3433

Was never voted on. Any reason?


Josh Cranwell comments:

If I'm honest John, I can't really remember.

I can't even see I've replied on there, so must have expired early.

I'm a graphic designer doing a developers job, and I think I get caught up in the thick of it at times, sorry I let that one go.

2012-01-27

Fahad Murtaza answers:

Hi Josh

(prefix)_usermeta is the wordpress MySQL table which you need to look into.

The keys are all saved there.

Then simply use this function for profile.

<?php
$user_id = 9; // which you can get dynamically
$key = 'last_name'; // last_name be replaced by your own key which you got from the MySQL table
$single = true;
$user_last = get_user_meta( $user_id, $key, $single );
echo '<p>The '. $key . ' value for user id ' . $user_id . ' is: ' . $user_last . '</p>';
?>


I hope it gets you started, please ask for more help after trying these.


Fahad Murtaza comments:

One question, do you want them on front end in the theme i.e user profile page on front end of the site?


Josh Cranwell comments:

I thought that maybe the case - I don't have access to MySQL table. Can it be seen anywhere from admin?


Fahad Murtaza comments:

Yes it can be seen from admin. Just give me wp-admin access.


Josh Cranwell comments:

Both.

I would like them to be in the back end user profile page.

But also on my front end user profile page.


Josh Cranwell comments:

I'm really interested to know how to find out the meta?

Do you mind hinting on how I can do this and I might be able to work the rest out thanks.


Fahad Murtaza comments:

I need to know the exact names of keys in the SQL table I mentioned. I can get it through wp-admin only. The reason being, I am not sure how the Gravity forms save data or else I would have known without even looking into DB.

About meta, I already provided you code in which you will just need to replace certain information as documented in the comments.


Fahad Murtaza comments:

OK Josh

See what Francisco says. His and mine solutions can work together. For looking into db keys just use that.

2012-01-27

Francisco Javier Carazo Gil answers:

Hi Josh,

You can use the author.php into [[LINK href="http://wordpress.org/extend/themes/twentyten"]]Twenty Ten[[/LINK]] as a guide. As you can see, in author page you can use this function: [[LINK href="http://codex.wordpress.org/Function_Reference/get_the_author_meta"]]get_the_author_meta[[/LINK]].

The use is simple:
* get_the_author_meta('language');
* Or if you want to get the value into screen with an echo: the_author_meta('language');


Francisco Javier Carazo Gil comments:

To see the meta keys that Gravity Forms use, directly with a file of your template do the next:


$authors_meta = $wpdb->get_results(
"
SELECT umeta_id, user_id, meta_key, meta_value
FROM $wpdb->usermeta;
"
);

foreach ( $authors_meta as $author_meta )
{
echo $author_meta->meta_key . " - " . $author_meta->meta_value . "<br/">;
}


Josh Cranwell comments:

Sorry Francisco

Your going to have to bear with me, where do I put this?

Will this make that data appear in my control panel user profile page?

I'm not very good when it comes to this part of wordpress :/