November 15, 2014 - Shortcodes

[user_meta] shortcode

Displays user meta data in a user-defined template
Note that the Formidable Pro plugin also has a [user_meta] that is internal to set the default value of a field.

The user metadata is stored in the "wp_usermeta" table - this is a standard WP table, but any amount of user data can be added.  This shortcode does not provide a way to add data, but our most common way will be via the CSV import user or some other plugin such as User Meta Display

Usage:  [user_meta template="class_rank_template" key="" user="4"]

  • "user" is only used to refer to some user other than the currently logged-in user - can be their user number or their login name (CampusID)
  • "template" specifies the _options entry name
  • "key" is a usermeta table entry "meta_key" entry

Either "template" OR "key" is used, not both - "template" takes precedence.

The "template" is stored in the "wp_options" data table[1]  or embedded directly inside the shortcode as the contents (example below).

Examples

To show the currently logged in user’s nickname:

[user_meta key="nickname"]
Class rank example:

Import a CSV file with columns "user_login", "class_rank", "class_rank_size", "class_rank_date"

Create an entry in the _options table with option_name "class_rank_template" and the option_value as:

<h3>Class Ranking</h3>
<script type="text/javascript">
function cr_showRank() {
  document.getElementById('showrank').value='{$class_rank}  of {$class_rank_size}';
}
</script>
Your class ranking as of {$class_rank_date}: <input type="button" name="showrank" id="showrank" style="color: #777777;" value="Show Ranking" onclick="cr_showRank()" />

Then, in the page to display this, enter the shortcode:

[user_meta template="class_rank_template"]

The values for the current logged-in user will be retrieved and substituted into the {$variable} spots in the template, and the resulting code will be inserted into the page (if values are found)

Meta data not found

If no requested meta data is found for that user, no output is generated

Optionally, a "not found" message can be appended onto the end of the template using two vertical bars (‘||’) as a separator:

Hi, {$nickname} || No nickname found

Just omit the "not found" message to show no text when meta values are not found.

Note that templates can contain shortcodes:

Here we have a [profile_link] shortcode inside the [user_meta] template:

Your Faculty Advisor is: [profile_link user_id="{$faculty_advisor_id}"]

Optional in-line template

Instead of using the _options table, templates may also be included as the content within a shortcode:

[user_meta]Your Faculty Advisor is: [profile_link user_id="{$faculty_advisor_id}"][/user_meta]

[1]  in our installs, the "wd_" is changed to "wdpr_ " for hack protection, so our table is renamed to "wdpr_options"

Comments are closed.