New! TypeRocket v5 is now available. See docs.
Access all of Typerocket. Get Pro.
User Profiles with Custom Fields
( v4 )
Expanding user profiles is easy with TypeRocket 3.0. To add custom fields to a user profile in WordPress all you need to use is the TypeRocket action hook tr_user_profile
and set a callback.
Add a "Job Title" and "Photo Gallery" so users can add more information about themselves.
Take a look at the code.
add_action('tr_user_profile', function($user) {
$form = tr_form();
echo $form->text('Job Title');
echo $form->gallery('Photo Gallery');
});
And that is it. Five lines of code and you have custom profile fields.
How it works
The action tr_user_profile
is a callback for both the WordPress hooks edit_user_profile
and show_user_profile.
This sets the form on the edit screen for other users accounts and your own.
Debug mode hints
Turn on debug mode and then you can copy the code hints found next to the fields you just added. This will help out when you are adding the custom fields to your theme templates.
Templating
There are three ways to access the user data from these new fields.
- By, the current user.
- Inside "The Loop".
- By user ID.
Here you have some example code you might write inside a template file:
<p><?php echo tr_users_field("job_title"); ?></p>
<?php
$gallery = tr_users_field("photo_gallery");
foreach($gallery as $image) {
echo wp_get_attachment_image($image);
}
?>
For the "Job Title" you can simply echo the string that is returned. For image and gallery fields, you can't simply echo the string.
To get the images from the gallery you need to loop through the image attachment ID's that are returned, use wp_get_attachment_image()
and echo them one at a time.
Current user
Since this code is not inside "The Loop", or on the user profile page, TypeRocket will gets the current user's information. This is TypeRockets default setting.
The Loop Author
If you call this code from within "The Loop" it will get the authors information.
<?php while(have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<section>
<?php the_content(); ?>
<p>Job Title <?php echo tr_users_field("job_title"); ?></p>
<p>
<?php
$gallery = tr_users_field("photo_gallery");
foreach($gallery as $image) {
echo wp_get_attachment_image($image);
}
?>
</p>
</section>
<?php endwhile; ?>
Specifying a user
What if you need that data for a different user? Just set the users ID as the second argument of tr_users_field()
.
<p>Job Title <?php echo tr_users_field("job_title", 1); ?></p>
<p>
<?php
$gallery = tr_users_field("photo_gallery", 1);
foreach($gallery as $image) {
echo wp_get_attachment_image($image);
}
?>
</p>
By adding the ID you override the automated ID detection and you get the user data you want. This works even if you call it in "The Loop" or any other context.
Note: TypeRocket has three automations for user fields. First, it looks for the global $user_id. If that is not present it looks for the post authors ID. If that is not present it looks for the currently logged in users ID.
Found a typo? Something is wrong in this documentation? Fork and edit it!