Categories

Get User Custom Field Values

Author: Scott Reilly
Version: 3.3
First released: 2011-08-08
Last update: 2021-11-21
Compatibility: WP 4.6 – 5.8.10
Download: [ zip ]
Description:

Use widgets, shortcodes, and/or template tags to easily retrieve and display custom field values for users.

Extended Description

This plugin provides a powerful widget, shortcode (with shortcode builder tool), and template tags for easily retrieving and displaying custom field values for the currently logged in user or any specified user.

This plugin provides functionality similar to the Get Custom Field Values plugin, but for user custom fields (which WordPress manages in a separate database table).

This plugin does NOT help you in setting user custom field values, nor does it provide an interface to list or otherwise manage user custom fields.

The list of useful user custom field values that are provided by default in WordPress are:

  • first_name
  • last_name
  • nickname
  • description
  • aim
  • yim
  • jabber

It is up to other plugins or custom code to add additional user custom fields that you may then be able to retrieve with this plugin.

Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage

Developer Documentation

Developer documentation can be found in DEVELOPER-DOCS.md. That documentation covers the numerous template tags, hooks, and shortcode provided by the plugin.

As an overview, these are the template tags provided the plugin:

  • c2c_get_current_user_custom() : Template tag to get custom fields for the currently logged in user.
  • c2c_get_author_custom() : Template tag to get custom fields for the current author (when on the permalink page for a post, page, or in a loop).
  • c2c_get_user_custom() : Template tag to get custom fields for a specified user.

These are the hooks provided by the plugin:

  • c2c_get_current_user_custom : An alternative approach to safely invoke c2c_get_current_user_custom() in such a way that if the plugin were deactivated or deleted, then your calls to the function won’t cause errors in your site.
  • c2c_get_author_custom : An alternative approach to safely invoke c2c_get_author_custom() in such a way that if the plugin were deactivated or deleted, then your calls to the function won’t cause errors in your site.
  • c2c_get_user_custom : An alternative approach to safely invoke c2c_get_user_custom() in such a way that if the plugin were deactivated or deleted, then your calls to the function won’t cause errors in your site.
  • c2c_get_user_custom_field_values_shortcode : Filter to customize the name of the plugin’s shortcode.
  • c2c_get_user_custom-user_field_proxy : Filter to prevent proxying to user object fields if no value for the custom field was found for the user.
  • get_user_custom_field_values/can_author_use_shortcodes : Filter to customize if post author can make use of the ‘user_custom_field’ shortcode.
  • get_user_custom_field_values/show_metabox : Filter to customize if the shortcode builder metabox is shown.

The shortcode provided is , which has a number of attributes to customize its behavior and output.

Find out more at the plugin’s WordPress Plugin Repository page.

Screenshots

Click to see full-size image.

  1. Screenshot of the 'Get User Custom' widget.

    Screenshot of the ‘Get User Custom’ widget.

  2. Screenshot of the 'Get User Custom' shortcode builder (not available in the block editor, aka Gutenberg).

    Screenshot of the ‘Get User Custom’ shortcode builder (not available in the block editor, aka Gutenberg).

Installation

  1. Install via the built-in WordPress plugin installer. Or install the plugin code inside the plugins directory for your site (typically /wp-content/plugins/).
  2. Activate the plugin through the ‘Plugins’ admin menu in WordPress
  3. Optional: Add filters for ‘the_user_meta’ to filter user custom field data (see the end of the file for commented out samples you may wish to include). And/or add per-meta filters by hooking ‘the_user_meta_$field’
  4. Give a user a custom field with a value, or have user custom fields already defined. (This generally entails use of plugin(s) that utilize the user custom fields feature built into WordPress. By default, in a practical sense WordPress only sets the ‘first_name’, ‘last_name’, and ‘nickname’ user custom fields, so you could try using one of them, even if just for testing even though WordPress provides functions to get those particular fields.)
  5. Optional: Use the provided ‘Get User Custom’ widget -or-
    Use the available shortcode in a post or page -or-
    Use the function c2c_get_current_user_custom() if you wish to access user custom fields for the currently logged
    in user. Use the function c2c_get_user_custom() to access user custom fields for a specified user. Use the function
    c2c_get_author_custom() to access custom fields for the current author (when on the permalink page for a post, page, or
    in a loop). Prepend either of the three mentioned functions with ‘echo’ to display the contents of the custom field; or
    use the return value as an argument to another function.

Release Log

3.3 (2021-11-09)

  • Change: Prevent users who cannot post unfiltered HTML from using the shortcode in posts. Props Francesco Carlucci and Erwan.
    • Hardens security to prevent potentail information disclosure or XSS by authors with limited privileges
    • New: Add shortcode class function can_author_use_shortcodes()
    • New: Add filter 'get_user_custom_field_values/can_author_use_shortcodes'
    • Change: Prevent shortcodes created by users who cannot publish unfiltered HTML from being evaulated
    • Change: Prevent display of shortcode builder metabox to users who cannot publish posts
  • New: Add filter 'get_user_custom_field_values/show_metabox' to customize if shortcode builder metabox is shown
  • New: Add DEVELOPER-DOCS.md and move template tag and shortcode documentation into it
  • Change: Note compatibility through WP 5.8+
  • Change: Update copyright date (2021)
  • Change: Update shortcode builder widget to 008
  • Change: Tweak installation instructions
  • Unit tests:
    • Change: Split shortcode-related tests out into their own file
    • Change: Split widget-related tests out into their own file
    • New: Add unit tests for show_metabox()
    • New: Add helper function create_post()
    • Change: Restructure unit test file structure
      • Change: Move phpunit/bin/ to tests/bin/
      • Change: Move phpunit/bootstrap.php into tests/phpunit/
      • Change: Move tests from phpunit/tests/ to tests/phpunit/tests/
      • Change: In bootstrap, store path to plugin file constant so its value can be used within that file and in test file
      • Change: In bootstrap, check for test installation in more places and exit with error message if not found
      • Change: Remove ‘test-‘ prefix from unit test files
  • New: Add a few more possible TODO items

3.2.2 (2020-09-18)

  • Change: Restructure unit test file structure
    • New: Create new subdirectory phpunit/ to house all files related to unit testing
    • Change: Move bin/ to phpunit/bin/
    • Change: Move tests/bootstrap.php to phpunit/
    • Change: Move tests/ to phpunit/tests/
    • Change: Rename phpunit.xml to phpunit.xml.dist per best practices
  • Change: Note compatibility through WP 5.5+

3.2.1 (2020-06-07)

  • Change: Update shortcode builder widget to 007:
    • New: Store object instantiated during register()
    • Change: Cast return value of c2c_get_user_custom_field_values_post_types filter as an array
    • Change: Sanitize string used in markup attributes (hardening)
  • New: Add TODO.md and move existing TODO list from top of main plugin file into it (and added to it)
  • Change: Note compatibility through WP 5.4+
  • Change: Update links to coffee2code.com to be HTTPS
  • Change: Update link to Get Custom Field Values plugin to point to wordpress.org instead of my site
  • Change: Unit tests: Use HTTPS for link to WP SVN repository in bin script for configuring unit tests (and delete commented-out code)

Copyright & Disclaimer

Copyright © 2011-2024 by Scott Reilly (aka coffee2code)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Discussion / Support

Have any questions, comments, or suggestions? Please provide them via the plugin’s WordPress.org support forum. I’ll do my best to reply in a timely fashion and help as best I can.

Unfortunately, I cannot provide guaranteed support, nor do I provide support via any other means.

Was this plugin useful useful to you? Consider giving it a rating. If you’re inclined to give it a poor rating, please first post to the support forum to give me a chance to address or explain the situation.

2 replies on “Get User Custom Field Values”

Leave a Reply

Your email address will not be published.