Plugin / Get User Custom Field Values

Scott Reilly

Description

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 | Author Homepage

Template Tags

The plugin provides three optional template tags for use in your theme templates.

Functions

  • <?php function c2c_get_current_user_custom( $field, $before='', $after='', $none='', $between='', $before_last='' ) ?>
    This allows access to custom fields for the currently logged in user. If the current visitor is NOT logged in, then the $none value is returned.

  • <?php function c2c_get_author_custom( $field, $before='', $after='', $none='', $between='', $before_last='' ) ?>
    This allows access to custom fields for the current author (when on the permalink page for a post, page, or in a loop).

  • <?php function c2c_get_user_custom( $user_id, $field, $before='', $after='', $none='', $between='', $before_last='' ) ?>
    This allows access to custom fields for any user specified by the $user_id value.

Arguments

  • $user_id
    (only for c2c_get_user_custom()) The integer value of the user’s id.

  • $field
    The name of the user custom field to display.

  • $before
    (optional) The text to display before all field value(s).

  • $after
    (optional) The text to display after all field value(s).

  • $none
    (optional) The text to display in place of the field value should no field value exists; if defined as ” and no field value exists, then nothing (including no $before and $after) gets displayed.

  • $between
    (optional) The text to display between multiple occurrences of the custom field; if defined as ”, then only the first instance will be used.

  • $before_last
    optional) The text to display between the next-to-last and last items listed when multiple occurrences of the custom field exist; $between MUST be set to something other than ” for this to take effect.

Examples

  • <?php c2c_get_current_user_custom('first_name'); ?>
    “Scott”

  • <?php c2c_get_current_user_custom('favorite_colors', 'Favorite colors: '); /* Where the 'favorite_colors' user custom field has been defined with values ?>
    “Favorite colors: blue, gray, green, black, red”

  • <?php c2c_get_current_user_custom('favorite_colors', 'Favorite colors: <ul><li>', '</li></ul>', '', '</li><li>'); ?>
    “Favorite colors:

  • blue
  • gray
  • green
  • black
  • red

  • <?php echo c2c_get_user_custom(3, 'first_name', 'Hi, ', '. Welcome back.'); // where 3 is the id of the user we want ?>
    “Hi, Scott. Welcome back.”

Filters

The plugin exposes five filters for hooking. Typically, customizations utilizing these hooks would be put into your active theme’s functions.php file, or used by another plugin.

c2c_get_current_user_custom (filter)

The ‘c2c_get_current_user_custom’ hook allows you to use 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.

Arguments:

  • same as for c2c_get_current_user_custom()

Example:

Instead of:

<?php $twitter = c2c_get_current_user_custom( 'twitter' ); ?>

Do:

<?php $twitter = apply_filters( 'c2c_get_current_user_custom', 'twitter' ); ?>

c2c_get_author_custom (filter)

The ‘c2c_get_author_custom’ hook allows you to use 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.

Arguments:

  • same as for c2c_get_author_custom()

Example:

Instead of:

<?php $aim = c2c_get_author_custom( 'aim', 'AIM: ' ); ?>

Do:

<?php $aim = apply_filters( 'c2c_get_author_custom', 'aim', 'AIM: ' ); ?>

c2c_get_user_custom (filter)

The ‘c2c_get_user_custom’ hook allows you to use 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.

Arguments:

  • same as for c2c_get_user_custom()

Example:

Instead of:

<?php $address = c2c_get_user_custom( 5, 'address' ); ?>

Do:

<?php $address = apply_filters( 'c2c_get_user_custom', 5, 'address ); ?>

c2c_get_user_custom_field_values_shortcode (filter)

The ‘c2c_get_user_custom_field_values_shortcode’ hook allows you to define an alternative to the default shortcode tag. By default the shortcode tag name used is ‘user_custom_field’. It is recommended you only utilize this filter before making use of the plugin’s shortcode in posts and pages. If you change the shortcode tag name, then any existing shortcodes using an older name will no longer work (unless you employ further coding efforts).

Arguments:

  • $shortcode (string)

Example:

// Use a shorter shortcode: i.e. [ucf field="last_name" /]
add_filter( 'c2c_get_user_custom_field_values_shortcode', 'change_c2c_get_user_custom_field_values_shortcode' );
function change_c2c_get_user_custom_field_values_shortcode( $shortcode ) {
    return 'ucf';
}

c2c_get_user_custom-user_field_proxy (filter)

The ‘c2c_get_user_custom-user_field_proxy’ hook allows you to prevent proxying to user object fields if no custom value for the custom field was found for the user. By default, if a user does not have a value for the given custom field, the plugin will compare the field name to the small list of user object fields (i.e. user table fields) to see if it is a valid user field. If so, then the user field value (as opposed to custom field value) is returned.

Arguments:

  • $allow_proxy (boolean) Default of true.

Example:

// Prevent user field proxying: i.e. this would not return anything: [user_custom_field field="user_email" user_id="1" /]
add_filter( 'c2c_get_user_custom-user_field_proxy', '__return_false' );

Shortcode

This plugin provides one shortcode that can be used within the body of a post or page. The shortcode is accompanied by a shortcode builder (see Screenshots) that presents a form for easily creating a shortcode. However, here’s the documentation for the shortcode and its supported attributes.

user_custom_field

The only shortcode provided by this plugin is named user_custom_field. It is a self-closing tag, meaning that it is not meant to encapsulate text. Except for ‘field’, all attributes are optional, though you’ll likely need to provide a couple to achieve your desired result.

The name of the shortcode can be changed via the filter ‘c2c_get_user_custom_field_values_shortcode’ (though making this customization is only recommended for before your first use of the shortcode, since changing to a new name will cause the shortcodes previously defined using the older name to no longer work).

Attributes:

  • field : (string) The name of the user custom field key whose value you wish to have displayed.
  • id : (string) The text to use as the ‘id’ attribute for a ‘span’ tag that wraps the output
  • class : (string) The text to use as the ‘class’ attribute for a ‘span’ tag that wraps the output
  • this_post : (boolean) Get the custom field value for the author of the post containing this shortcode? Takes precedence over user_id attribute. Specify 1 (for true) or 0 for false. Default is 0.
  • user_id : (integer) ID of user whose custom field’s value you want to display. Leave blank to search for the custom field for the currently logged in user. Use 0 to indicate it should only work on the permalink page for a page/post.
  • before : (string) Text to display before the custom field.
  • after : (string) Text to display after the custom field.
  • none : (string) Text to display if no matching custom field is found (or it has no value). Leave this blank if you don’t want anything to display when no match is found.
  • between : (string) Text to display between custom field items if more than one are being shown. Default is ‘, ‘.
  • before_last : (string) Text to display between the second to last and last custom field items if more than one are being shown.

Examples:

  • Get nickname for current post’s author
    [user_custom_field field=”nickname” this_post=”1″ /]

  • Get AIM account name for a specific user
    [user_custom_field field=”aim” user_id=”2″ /]

  • Wrap post author’s bio in markup, but only if the author has a bio.
    [user_custom_field field=”description” before=”My bio:” /]

DIRECTORY DISCLAIMER

The information provided in this THEME/PLUGIN DIRECTORY is made available for information purposes only, and intended to serve as a resource to enable visitors to select a relevant theme or plugin. wpSocket gives no warranty of any kind, express or implied with regard to the information, including without limitation any warranty that the particular theme or plugin that you select is qualified on your situation.

The information in the individual theme or plugin displayed in the Directory is provided by the owners and contributors themselves. wpSocket gives no warranty as to the accuracy of the information and will not be liable to you for any loss or damage suffered by you as a consequence of your reliance on the information.

Links to respective sites are offered to assist in accessing additional information. The links may be outdated or broken. Connect to outside sites at your own risk. The Theme/Plugin Directory does not endorse the content or accuracy of any listing or external website.

While information is made available, no guarantee is given that the details provided are correct, complete or up-to-date.

wpSocket is not related to the theme or plugin, and also not responsible and expressly disclaims all liability for, damages of any kind, arising out of the use, reference to, or reliance on, any information or business listed throughout our site.

Keep Leading Your Followers!
Share it for them.