How to Create a Custom WordPress Shortcode to Display User Meta Fields

By: John Louwe

Published: November 11, 2024

Hey there! I’m John, a web developer specializing in WordPress, though I work with other CMS platforms too. In this guide, I’ll show you how to create a custom WordPress shortcode to display user meta fields on your website. Customizing your site based on user data is a fantastic way to personalize the experience, and using meta fields makes it possible to store and retrieve all sorts of custom information.

Let’s dive into creating a shortcode to pull user meta data and display it wherever you need. If you’re working with custom fields in WordPress, you’ll find this handy. I’ll also share why I love using JetEngine from Crocoblock—it’s one of the best tools out there for adding and managing meta fields on WordPress.

What Are Meta Fields in WordPress?

In WordPress, meta fields (or custom fields) store additional information about your posts, users, or any content type. These fields can store anything from basic profile data to complex, custom settings added by plugins. Accessing and displaying this data often requires a bit of coding, but once you have a shortcode ready, showing this information on your WordPress site is a breeze.

One of my go-to tools for managing custom fields is JetEngine by Crocoblock. It’s a powerful WordPress plugin that makes adding and managing meta fields easy, even for complex content needs. JetEngine lets you create custom fields and display them with flexibility, making it a staple for developers working with dynamic content.

Creating the Shortcode to Display User Meta Data

To add this shortcode to your WordPress site, you have a couple of options. Traditionally, you would paste the code into your theme’s functions.php file. However, I prefer using the WP Code Snippet plugin to add custom code snippets. It’s a safer method, as it prevents issues if you switch themes or need to manage multiple custom codes. With WP Code Snippet, you can easily activate, deactivate, or edit snippets without risking errors in your functions.php.

Here’s the code for our custom shortcode:

function custom_usermeta_shortcode( $atts ) {
    $atts = shortcode_atts(
        array(
            'key' => '',        // Meta key to retrieve
            'default' => '0'    // Default value if meta does not exist
        ),
        $atts
    );

    // If no meta key is provided, display an error message
    if ( empty( $atts['key'] ) ) {
        return 'No meta key provided.';
    }

    // Get the ID of the current logged-in user
    $user_id = get_current_user_id();

    // Retrieve the user meta value
    $usermeta = get_user_meta( $user_id, $atts['key'], true );

    // Return the user meta if it exists, otherwise return the default value
    return !empty($usermeta) ? $usermeta : $atts['default'];
}
add_shortcode( 'custom_usermeta', 'custom_usermeta_shortcode' );

How to Use the Shortcode in WordPress

Once the code is added via the WP Code Snippet plugin, using the shortcode is simple. You can add it to any page or post to display a specific user meta field:

[custom_usermeta key="your_meta_key"]

If you want to set a custom default value (instead of 0), here’s how you can do it:

[custom_usermeta key="your_meta_key" default="No data available"]

Real-Life Applications for Custom Meta Fields

  • User Profile Pages: This shortcode is perfect for displaying profile details stored in custom fields.
  • Dynamic Content Display: Meta fields allow you to store a wide range of data, and the shortcode lets you display it dynamically. For advanced management of custom fields, JetEngine is my go-to solution. It offers flexibility in creating, organizing, and displaying meta fields across WordPress sites, making it ideal for developers and site owners.
  • Personalized Messages: You can also use this shortcode to add custom greetings or calls-to-action for logged-in users based on their profile settings.

Wrapping It Up

Adding custom fields to your WordPress site is an effective way to create a personalized user experience. This shortcode allows you to display any user-specific data stored in meta fields, adding a unique touch to your site’s content. And, by using WP Code Snippet, you can manage your custom code more safely and flexibly than modifying functions.php.

If you’re serious about using custom fields and meta data, I highly recommend trying JetEngine. It’s a robust tool for creating and managing meta fields, making the whole process simpler and more efficient.

I hope you found this guide helpful! Let me know if you have any questions, and happy coding!

Looking to Buy Property in Cebu, Philippines?

Cebu Real Estate Optin

Recent Blogs

How to Display URL Parameters in WordPress Using Shortcodes

In this tutorial, we’ll explore two simple yet powerful shortcodes that you can use to display the URL parameters of the current page in WordPress. Whether you want to show a raw query string or a nicely formatted list of URL parameters, this guide will help you implement both solutions seamlessly. The Backstory This solution…

How to Create a Custom WordPress Shortcode to Display User Meta Fields

Learn how to create a custom WordPress shortcode to display user meta fields easily. Discover tips for safely adding code with WP Code Snippet, plus see why JetEngine is the best tool for managing custom fields on your site!

Enhance Your BuddyBoss Login Page with BuddyCTA Plugin

Hello there! I’m John Louwe, a WordPress developer and a membership expert with a passion for crafting exceptional online experiences. Today, I’m excited to introduce you to a game-changing WordPress plugin that’s going to revolutionize your BuddyBoss login pages – the BuddyCTA Plugin! Meet John Louwe: Your WordPress Guru Before we dive into the magic…