Skip to content

Latest commit

 

History

History
109 lines (72 loc) · 3.37 KB

user-profiles-with-custom-fields.md

File metadata and controls

109 lines (72 loc) · 3.37 KB

Title: User Profiles with Custom Fields Description: Add fields to user profiles with the action hook tr_user_profile.


Getting Started

To add custom fields to a user profile in WordPress, all you need to use is the TypeRocket action hook tr_user_fields and set a callback. Add a "Job Title" and "Photo Gallery" so users can add more information about themselves.

Custom User Profile Fields

Take a look at the code.

add_action('typerocket_user_fields', function($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_fields 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 user's 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 user data from these new fields.

  1. By the current user.
  2. Inside "The Loop".
  3. By user ID.

Here you have some example code you might write inside a template file:

<p><?php echo tr_user_field("job_title"); ?></p>

<?php
$gallery = tr_user_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 get the current user's information. This is TypeRocket's default setting.

The Loop Author

If you call this code from within "The Loop" it will get the author's information.

<?php while(have_posts()) : the_post(); ?>
    <h1><?php the_title(); ?></h1>
    <section>
        <?php the_content(); ?>

        <p>Job Title <?php echo tr_user_field("job_title"); ?></p>

        <p>
            <?php
            $gallery = tr_user_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 user's ID as the second argument of tr_user_field().

<p>Job Title <?php echo tr_user_field("job_title", 1); ?></p>

<p>
    <?php
    $gallery = tr_user_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.