20
Mar, 2015
Mar, 2015
User Custom Fields via PHP Code
Table of Contents
Whenever you create a Custom Registration/Profile Field with s2Member, you are required to assign it a Unique ID. s2Member takes the Unique ID that you assign—replacing anything that is not a-z0-9
with an _
underscore. This sanitized Unique ID is used to reference the custom field in code.
See: WordPress Dashboard → s2Member → General Options → Registration/Profile Fields & Options
Using the get_user_field()
Function
s2Member adds a new function to WordPress called get_user_field()
. You can find the function source here.
This function accepts two arguments:
$unique_id
Required. The Unique ID that you want the value for.$user_id
Optional. Defaults to the current user ID; i.e., the ID of the user currently logged into the site.
Example PHP Code
<?php
$company = get_user_field('company');
// ↑ Pulls the value for a custom field with ID `company`, for the current user.
$user_id = 123; // A specific user ID.
$company = get_user_field('company', $user_id);
// ↑ Pulls the value for a custom field with ID `company`, for user ID 123.
Return Values for get_user_field()
- (mixed) The value of the requested field, or false if the field does not exist.
Using the get_user_option()
Function in WordPress
This function is part of the WordPress core. See: Function Reference/get user option « WordPress Codex
This function accepts two arguments:
$option_name
Required. Any WordPress user option name, or the Unique ID for a custom field generated with s2Member (optionally prefixed withs2_
). The optionals2_
prefix is suggested in order to avoid ambiguity.$user_id
Optional. Defaults to the current user ID; i.e., the ID of the user currently logged into the site.
Example PHP Code
<?php
$company = get_user_option('company'); // An s2Member custom field with ID `company`.
$company = get_user_option('s2_company'); // This works for s2Member custom fields too; prefix is optional.
// ↑ Pulls the value for a custom field with ID `company`, for the current user.
$user_id = 123; // A specific user ID.
$company = get_user_option('company', $user_id); // An s2Member custom field with ID `company`.
$company = get_user_option('s2_company', $user_id); // This works for s2Member custom fields too; prefix is optional.
// ↑ Pulls the value for a custom field with ID `company`, for user ID 123.
See also: [s2Get /]
Shortcode Documentation